Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all 49872 articles
Browse latest View live

[SOLVED] Filter function with evaluate

$
0
0
Hello everyone

I have names in the range A1:A10 and A1 has the name "Yasser" and A7 has the name "Ahmed" and other cells with other names
I have tried this line so as to filter and get the rows that has the value "Ahmed" or "Yasser"
Code:

    Dim x
   
    x = Filter([TRANSPOSE(IF(OR(A1:A10="Yasser",A1:A10="Ahmed"),ROW(1:10)))], False, False)

But when following the code I found x has all the rows and I expect to get 1 and 7 row only
Any help at this point.

Change a (F11) chart's axis values using cell values

$
0
0
Hi Guys,

I want to be able to edit axis of a chart (a "F11" chart not a chart within a sheet) by entering in numbers within cells.
See the "Chart1 tab" and "Chart2 tab"

The following code words for a chart within a sheet, but I dont know how to select a chart if that chart is it's own tab.

Code:

Option Explicit

Function setChartAxis(sheetName As String, chartName As String, MinOrMax As String, _
    ValueOrCategory As String, PrimaryOrSecondary As String, Value As Variant)

'create variables
Dim cht As Chart
Dim valueAsText As String

'Set the chart to be on the same worksheet as the function
'Set cht = Application.Caller.Parent.ChartObjects(chartName).Chart
Set cht = Application.Caller.Parent.Parent.Sheets(sheetName) _
    .ChartObjects(chartName).Chart


'Set Value of Primary axis
If (ValueOrCategory = "Value" Or ValueOrCategory = "Y") _
    And PrimaryOrSecondary = "Primary" Then

    With cht.Axes(xlValue, xlPrimary)
        If IsNumeric(Value) = True Then
            If MinOrMax = "Max" Then .MaximumScale = Value
            If MinOrMax = "Min" Then .MinimumScale = Value
        Else
            If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
            If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
        End If
    End With
End If

'Set Category of Primary axis
If (ValueOrCategory = "Category" Or ValueOrCategory = "X") _
    And PrimaryOrSecondary = "Primary" Then

    With cht.Axes(xlCategory, xlPrimary)
        If IsNumeric(Value) = True Then
            If MinOrMax = "Max" Then .MaximumScale = Value
            If MinOrMax = "Min" Then .MinimumScale = Value
        Else
            If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
            If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
        End If
    End With
End If

'Set value of secondary axis
If (ValueOrCategory = "Value" Or ValueOrCategory = "Y") _
    And PrimaryOrSecondary = "Secondary" Then

    With cht.Axes(xlValue, xlSecondary)
        If IsNumeric(Value) = True Then
            If MinOrMax = "Max" Then .MaximumScale = Value
            If MinOrMax = "Min" Then .MinimumScale = Value
        Else
            If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
            If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
        End If
    End With
End If

'Set category of secondary axis
If (ValueOrCategory = "Category" Or ValueOrCategory = "X") _
    And PrimaryOrSecondary = "Secondary" Then
    With cht.Axes(xlCategory, xlSecondary)
        If IsNumeric(Value) = True Then
            If MinOrMax = "Max" Then .MaximumScale = Value
            If MinOrMax = "Min" Then .MinimumScale = Value
        Else
            If MinOrMax = "Max" Then .MaximumScaleIsAuto = True
            If MinOrMax = "Min" Then .MinimumScaleIsAuto = True
        End If
    End With
End If

'If is text always display "Auto"
If IsNumeric(Value) Then valueAsText = Value Else valueAsText = "Auto"

'Output a text string to indicate the value
setChartAxis = ValueOrCategory & " " & PrimaryOrSecondary & " " _
    & MinOrMax & ": " & valueAsText

End Function

Transfer Data from Invoice to another excel sheet using vba

$
0
0
i am mudasser islam and from west bengal, india. i am new here and also new in vba. after watching different you tube videos i made this attached .xlsm file "Transfer Data from Invoice to another excel sheet using vba". but there is an error. i cant solve it. i also want to enter invoice total in sales sheet too. please help me.
Attached Files

Worksheet_BeforeDoubleClick 1004 error on worksheet selection

$
0
0
Hi guy's,

Hoping someone can help with a VBA problem I am having.

For the last 2 weeks I try a to get this code working. I would like a popup userform with a search box that pops up with a bubble click in column b. But I do something wrong and get a 1004 error on the red line below I don't understand why.
Does anyone know what i'm doing wrong? And is so kind to help me?

Code:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim uiChosen As String
    Dim MyList As Range
    Dim myPrompt As String

    If Target.Column <> 2 Then Exit Sub
   
    Set MyList = Sheets("RECEPT_7-12").Range("mylist")
    myPrompt = "kies een naam uit lijst"
    uiChosen = UserForm1.ChooseFromList(MyList, myPrompt, Default:=Target.Value, xlFilterStyle:=xlContains)

    If StrPtr(uiChosen) <> 0 Then
        Target.Value = uiChosen
        Cancel = True
    End If
End Sub

VBA to search all worksheets, for multiple keywords, and copy matched rows

$
0
0
Hi,
excel have find function with can find all cells matching one keyword in all worksheets, within workbook.
but I need to find all cells which consist of more than one keywords in all worksheets (i.e. search cells which match all keywords) ;

and copy all those rows, with matching cells, to a single worksheet named"searchedresult";

and if possible, have an extra columns in the searchedresult which have the hiperlink for each matching cell, which allow me to click and go to each particular matching cell when needed; name of worksheet found and cell address.

Would it be possible to have a VBA which will prompt to ask for keywords to be searched (each seperated by ";") then carry out the search and put results as described above.


thanks a lot.

patrick

Show Hide Text boxes & objects

$
0
0
I'm attempting to simulate drag racing lights (Objects?) and various randomly generated times in text boxes
I want the lights to run up and then down which they do (roughly but I can tidy that up later) by running Sub 3 Lights() in Module 3.
I can also make the various times appear about how I want them to by running Sub Race1_Heat1 in Module 9. However, when I try to run them within the Sub for Command Button 1 (Press here to Start Race x) the lights and text boxes don't show.

So, the final outcome should be:
Press here to Start Race x
Random numbers generated
Voice instruction
Lights run up from green to red, go out then run from Red to green. (Red goes out as amber comes on and so on to green staying on).
Race "starts"
Start time text box linked to cell BR6 shows
Delay
First time text box linked to cell BS6 shows
Delay
And so on 'til all 6 times are shown and the "Race" is over.

The attached workbook has my attempts at making this work including putting the Text Box show in the Command Button 1 code and I'd be grateful if anyone could get this to work

By the by, I'd also like the Text Box to only show 5 numbers, two before the decimal point and three after (xx.xxx).

Many thanks

Frankie
Attached Files

Aligning all cells horizontal macro help needed.

$
0
0
Looking for help on a few things.

How to make all active cells go to the left, the sheet changes daily so I can't specify a range.

I thought the below would work but Excel no likey.

Code:

ActiveSheet.HorizontalAlignment = xlLeft
Also, once I run the below code I am left with the dotted lines and I drag them manually to the right side of the spreadsheet. What would I need to do to reset this ?

Code:

Activesheet.ResetAllPageBreaks
Thanks.

General assistance needed using VAA for Excel

$
0
0
Question 1: What is the most quickest way to mark my code within a modsheet as a comment?
Question 2: I've added a button to an Excel spreadsheet but where do I find the properties for that button? I found that I could find properties for buttons added to a userform but not buttons added directly to a spreadsheet.

Thanks so MUCH for you generous assistance!

Change a worksheet i goto error

$
0
0
I have the following loop in a Macro. I need to have the ON Error GoTo Label to Increase the ws by 1 and resume.
Code:

With Worksheets("setup")
        For Each cell In .Range("A11", .Range("A" & Rows.count).End(xlUp))
            Set ws = Worksheets(cell.Value)
            On Error GoTo Errorone
            'copy the constant info
            .Range("A10:G10").Copy ws.Range("A10:G10")
            'copy score
            cell.Offset(0, 1).Copy ws.Range("B10")
            'shift it all down
            ws.Rows(10).Insert Shift:=xlDown
            'get the formuls
            ws.Range("H12:L12").Copy ws.Range("H11:L11")
            counter = counter + 1
            Next cell
    End With

select cell and have content in another cell changed

$
0
0
Hello everyone. First of all I hope you are all safe and well with what is going on now in the world.

I have a request, anyone can help me build a code to do the below?

Anytime I click on a cell of range
D6:H26

and the content of the cell is either Blank or a "?" (blank or ? are always results of a formula, they are not values specifically entered in the cell. The other possible 2 outcomes of the formula are a V or an X.)

a form comes up and allows me to:

Option 1
Click on a green V
Option 2
Click on a red X
Option 3
Close the form.

My range D6:H26 is basically a chart, with fixed headers in row 5 and fixed values in column B.


So let's say that I click on cell E9.

A form comes up. I click on the "V".
Now what I need is that I get a Msgbox to read "Do you want to assign [value from B9] to [value from E5]?

Then a simple yes and no. With No to be the pre-selected one.

If Yes is selected then E9 remains as is, but the code changes the value of CQ9 to V.


So basically, it's the correspondent cell in range CP6:CT26 that changes the value according to my selection. This range is hidden.


If I click on the "X" instead, the Msgbox has to read "Do you want to exclude [value from B9] from [value from E5]?

If Yes is selected then the code changes the value of CQ9 to X.


I am pretty sure it's quite simple but I get lost with the form and the references. So I'd rather learn from any of you.


Thank you in advance! If something of the above is unclear do let me know

"Evaluate" VBA function

$
0
0
Hello. I am new to VBA and would appreciate some help.

I am writing a simple VBA program to determine if a range of cells has text or a number. Then populate another range of cells (same worksheeet) with the results.

Conditionals: If the cell is text then I want the cell = empty. If the cell is a number then I want the cell to return a 1 if value >0.85 or a 0 if value < 0.85.

The cells to evaluate are: A1 to AS60
The output cells are: AU1 to CM60


Code:

  Range("AU1:CM60").Formula = "=Evaluate("IF(ISTEXT(A1),"",IF(A1>0.85,1,0))")"
Error: "Expected: end of statement"

I've been struggling with this error for awhile and would appreciate some help.

Macro to query website via API and return value

$
0
0
I am trying to retrieve data from the CMS website to see if a doctor can order or refer a medical test for Medicare. Link for the API info is as follows:

https://dev.socrata.com/foundry/data.cms.gov/7yuw-754z

The field I am trying to retrieve is "eligible_to_order_and_refer". I registered an app token and have a bunch of NPI numbers in column A. Looking for the return value in Column B for "eligible_to_order_and_refer". Any ideas for a macro? Using Excel 365. Thanks.

Downloading table data from web

$
0
0
Hello,

My knowledge of VBA has limited my ability to do something, so I find myself here.

A few years ago, I used querytables (code lifted from a macro recording) to download table data from a website (baseball-reference.com). I downloading league-wide batting, pitching, and fielding statistics for every year, and the code worked beautifully, because I only had to specifiy the URL and table name in the code:

Code:

With Worksheets("Sheet1").QueryTables.Add(Connection:= "LINK", Destination:=Worksheets("Sheet1").Range("$A$1"))
The above is an example which will get you the data from the fielding table for first basemen in 1956. By substituting variables for the years and positions, I could get all data from every year, because despite the fact that the tables sometimes changed from page to page (meaning some columns were added to the pages for later years), the code simply pulled all data regardless.

This no longer works, as best I can tell, because I believe the table properties have been modified or changed. My queries now return nothing for the tables I'm considering. However, recording a macro for extracting data from web gives me code that returns the data as a listobject:

Code:

ActiveWorkbook.Queries.Add Name:="Player Fielding - 2B Table", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""LINK""))," & Chr(13) & "" & Chr(10) & "    Data5 = Source{5}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data5,{{""Rk"", Int64.Type}, {""Name"", type text}, {""Age"", Int64.Type}, {""Tm"", type text}, {""Lg"", type text}, {""G"", Int64.Type}, {""GS"", Int64.Type}" & _
        ", {""CG"", Int64.Type}, {""Inn"", type number}, {""Ch"", Int64.Type}, {""PO"", Int64.Type}, {""A"", Int64.Type}, {""E"", Int64.Type}, {""DP"", Int64.Type}, {""Fld%"", type number}, {""Rtot"", Int64.Type}, {""Rtot/yr"", Int64.Type}, {""Rtz"", Int64.Type}, {""Rdp"", Int64.Type}, {""RF/9"", type number}, {""RF/G"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add

    With Worksheets("Sheet2").ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Player Fielding - 2B Table"";Extended Properties=""""" _
        , Destination:=Worksheets("Sheet2").Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [Player Fielding - 2B Table]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Player_Fielding___2B_Table"
        .Refresh BackgroundQuery:=False
    End With


Now this works, but each column heading must be specified and when it runs into a table that is missing a column, or has an extra, it throws an error. This means I can't use this code for all the pages for all the years, and instead would have to constantly modify it. What I'd like to do is to be able to grab all of the data from these tables without specifying column headers, and was wondering if there was a way I could still do this, like the good ol' days. Seems there should be a way...any thoughts?

Thanks in advance.

ListBox _change() command doesn't execute

$
0
0
Hi,

In a worksheet, I created a MultiSelectMulti ActiveX ListBox called ListBox1. The code is in the target worksheet called Feuil1.

Part of the code in the _change() event handler does not to execute.

According to the documentation (docs . microsoft.com/en-us/office/vba/language/reference/user-interface-help/change-event), it should fire for the following:
  • Clicking a CheckBox
  • Selecting a new text value for a ListBox

As per below example, the MsgBox executes fine every time I un/select a row. Yet, weirdly, the .BackColor does not. I've to open the debugger and manually press F8 for it to execute.

Code:

Private Sub ListBox1_Change()
MsgBox "this isnt fun"
With Feuil1.ListBox1
.BackColor = RGB(255, 125, 255)
End With
Me.ListBox1.BackColor = RGB(0, 125, 0)
End Sub

Does someone have an idea as to why this happens?

Thanks,

Kevin

VBA Code Needed On 250 lines, code changes on each line, Eaiser way to do this?

$
0
0
So I'm a novice to VBA, used Excel for years self taught but trying to get my head around VBA coding.
I have written for a work delivery spreadsheet to send out automated texts when dispatching an order. However there are several references to cell numbers in the code (E8, M8, B8, K8) and I need to be able to change these for each button working down my spreadsheet.
I have about 50 per sheet & 5 sheets to do so looking for the fastest and easiest way to replicate the code without having to do 250 individual buttons & code.
Any help and advice is greatly appreciated.

Remove row range based on two criterias

$
0
0
Hi People,

Can someone give me a hand on this? Attached is an spreadsheet where I have two criteria A3 and B3. I want to filter my range D3 and I20 based on A3 and B3.
The yellow row must be removed but from ColD all the way to ColI because I have more information in the rest columns.

Note this should be in VBA.

Thanks in advance.

How to add borders help

$
0
0
Is there a way in VBA to put the 4 wall borders for every active cell in the currnet sheet without a range, I have tried the below but to no avail.

Code:

ActiveSheet.Borders.LineStyle = xlContinuous
Code:

ActiveSheet.BorderAround LineStyle:=xlContinuous
Thanks.

Send Email via Outlook

$
0
0
Hello,

I'm using some code to filter and copy data from Sheet1 to Sheet2.

I want to add a module to send email via Outlook.

As we see in the attachment (Sheet2). I want to copy this data into new workbook, from A1 cell to last data available in J column cell (data will vary - can be less or more).

In the attached the last data is till J16, this can be more or less.

Once the data is copied to new workbook, this should get attached to a new email into outlook.

(I've read, for sending emails through Outlook as attachment, first the attachment is saved to our system, then it is been killed and then it is attached to email):confused:

Once the email is popped with the attachment, I'll manually add the recipients names into to, to whom I want to send the emails.

Please suggest.

Thanks !
Attached Files

Time diffrence between two date stamps, faulty VBA

$
0
0
Dear Experts,

Could you please help me to repair my macro in VBA. The aim is to put in a column R the time difference in days ( can also show hours) between later date stamp column M and the first date stamp in column B. The macro attached below works and calculates the difference in days, however it runs the macro till the end of the column R. returning the value of 0, even when columns B and M are empty. I would like the calculation only to be done when there is a value in the time stamp cells in column B and M.
I would be grateful for yours suggestions.

Kind regards

Martek

Sub datediff()

With Range("R12:R" & Range("B" & Rows.Count).End(xlDown).Row)
.FormulaR1C1 = "=datedif(rc[-16],rc[-5],""d"")"
Dim x As Integer
Application.ScreenUpdating = False
' Set numrows = number of rows of data.
' Establish "For" loop to loop "numrows" number of times.
' Insert your code here.
' Selects cell down 1 row from active cell.

End With
End Sub

How to Add Events to Calendar

Viewing all 49872 articles
Browse latest View live