I need to use a vlookup inside vba but the worksheetname constantly changes but can always be referenced as ActiveWorkbook.Worksheet(1) and ActiveWorkbook.Worksheet(2). So I used dim so all selection can refer to ws1 and ws2 but of course inside a vlookup formula that kind of script doesn't work. Hope anyone can help rewrite those vlookup formula lines.
It regards the last 5 lines with the ActiveCell.FormulaR1C1 lines where i need a solution for 'ws2'!
Thanks for the help.
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ActiveWorkbook.Worksheets(1)
Set ws2 = ActiveWorkbook.Worksheets(2)
ws1.Select
Selection.AutoFilter
Range("G2").Select
ActiveCell.FormulaR1C1 = "Web sales"
Range("H2").Select
ActiveCell.FormulaR1C1 = "Web stock"
Range("I2").Select
ActiveCell.FormulaR1C1 = "Total Sales"
Range("J2").Select
ActiveCell.FormulaR1C1 = "Total Stock"
Range("F2:F71").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-102
Range("G2:J71").Select
ActiveWindow.SmallScroll Down:=-66
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Columns("H:H").EntireColumn.AutoFit
Range("I5").Select
Application.CutCopyMode = False
Range("D3:D150").Select
ws2.Select
Range("D3:D150").Select
ws1.Select
ActiveWindow.SmallScroll Down:=-66
Range("G3").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-3],'ws2'!RC[-3]:R[42]C[-1],3,0)"
ActiveWindow.SmallScroll Down:=-30
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-3],ws2!R3C4:R45C6,3,0)"
Below are two examples from your original code showing how to include the name of worksheet into formula:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3]," & ws2.Name & "!RC[-3]:R[42]C[-1],3,0)"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3]," & ws2.Name & "!R3C4:R45C6,3,0)"
Related
I have been working with VBA for about two years now, and I am having a problem I haven't had before. I am trying to autofill columns A, B, D, E and G with formulas based on the number of cells that are populated in column L. In the example shown below, cells L2-L5 are populated. With my autofill formulas, this should mean that cells A2-A5 populate, cells B2-B5 populate etc. But they don't. Only column G populates correctly, and I am stumped as to why this is happening. Any brilliant ideas?
Here is my code:
Application.DisplayAlerts = False
Sheets("QBTimecard").Select
Range("DP2").Select
ActiveCell.FormulaR1C1 = _
"=RC[-119]&""_""&RC[-107]&""_""&IF(RC[-2]<>""Client Transportation"",""Z"",""XY"")"
Range("DQ2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-13]<=RC[-8],""Pass"",""MMV"")"
On Error Resume Next
Selection.Autofill Destination:=Range("dp2:ds" & Cells(Rows.Count, "A").End(xlUp).Row)
Sheets("QBTimecard").Select
Columns("DP:DP").Select
Selection.Copy
Sheets("PunchEntryImport").Select
Columns("L:L").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("L1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Blank"
Range("L2").Select
Sheets("PunchEntryImport").Select
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(QBTimecard!C,MATCH(PunchEntryImport!RC[11],QBTimecard!C[119],0))"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=SUMIF(QBTimecard!C[118],PunchEntryImport!RC[10],QBTimecard!C[13])/COUNTIF(QBTimecard!C[118],PunchEntryImport!RC[10])"
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(Lookup!C[-2],MATCH(INDEX(QBTimecard!C[9],MATCH(PunchEntryImport!RC[8],QBTimecard!C[116],0)),Lookup!C[-3],0))&""/1/""&INDEX(QBTimecard!C[10],MATCH(PunchEntryImport!RC[8],QBTimecard!C[116],0))&"" ""&TEXT(INDEX(QBTimecard!C[12],MATCH(PunchEntryImport!RC[8],QBTimecard!C[116],0)),""HHMM"")"
Range("E2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(Lookup!C[-3],MATCH(INDEX(QBTimecard!C[8],MATCH(PunchEntryImport!RC[7],QBTimecard!C[115],0)),Lookup!C[-4],0))&""/1/""&INDEX(QBTimecard!C[9],MATCH(PunchEntryImport!RC[7],QBTimecard!C[115],0))&"" ""&TEXT(INDEX(QBTimecard!C[12],MATCH(PunchEntryImport!RC[7],QBTimecard!C[115],0)),""HHMM"")"
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(QBTimecard!C[103],MATCH(PunchEntryImport!RC[5],QBTimecard!C[113],0))"
On Error Resume Next
'This is the portion that is not autofilling
Selection.Autofill Destination:=Range("a2:a" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("b2:b" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("d2:d" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("e2:e" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("g2:g" & Cells(Rows.Count, "L").End(xlUp).Row)
Without re-writing the whole thing, this is a more-approachable way to handle this, without the Select/ Activate/ Autofill:
Dim wsQBTC As Worksheet, wsPE As Worksheet, lr As Long
Set wsQBTC = Worksheets("QBTimecard")
Set wsPEI = Worksheets("PunchEntryImport")
lr = wsQBTC.Cells(Rows.Count, "A").End(xlUp).Row
With wsQBTC
.Range("DP2:DP" & lr).FormulaR1C1 = "=RC[-119]&""_""&RC[-107]&""_""&IF(RC[-2]<>""Client Transportation"",""Z"",""XY"")"
.Range("DQ2:DQ" & lr).FormulaR1C1 = "=IF(RC[-13]<=RC[-8],""Pass"",""MMV"")"
End With
From the help for Range.AutoFill
The destination must include the source range.
Because you are using Selection.AutoFill and G2 is the active cell, the autofill will only work when G2 is part of the range being autofilled.
To fix this, replace Selection.AutoFill with Range("A2").AutoFill, Range("B2").AutoFill and so on
I have a table with data in it and run a macro to neaten things up and then adds a hyperlink to column G but the issue is when the macro has run, the date changes from:
https://websitenamehere.com//agentView/agentname#company/2021-11-08
to
https://websitenamehere.com//agentView/agentname#company/44508
In my table, I created in column H the column for "today" and then in column G is where it puts it all together but messing up the date part.
Here is my code which i am using. Any help would be appreciated.
Sub CleanFollowUps()
Dim Lrow As Integer
Dim lCol As Integer
Dim C As Range
Lrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(Lrow, lCol)), , xlYes).Name = "FollowUps"
Range("FollowUps[#All]").Select
ActiveSheet.ListObjects("FollowUps").TableStyle = ""
Range("F1").Select
ActiveCell.FormulaR1C1 = "Helper"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=IF(SUM([#[Due today]]+[#Late])>0,""Yes"","""")"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Schedule"
Range("H1").Select
ActiveCell.FormulaR1C1 = "day"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Columns("H:H").Select
Selection.NumberFormat = "yyyy-mm-dd"
Range("H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=""https://websitenamehere.com/agentView/""&[#Username]&""#company/""&[#day]"
Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Sheets("FU")
For Each C In .Range("G2:G" & .Range("G" & .Rows.Count).End(xlUp).Row)
.Hyperlinks.Add Anchor:=C, Address:=C.Value, SubAddress:=C.Value
Next C
End With
Application.DisplayAlerts = True
End Sub
I have below code to copy and paste from different sheets in workbook. I don't want to show the user different sheets opening and show only the sheet in which the action triggered (Interface).
How can I do it, any guidance?
Sheets("List").Select
Range("N2").Select
lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Selection.AutoFill Destination:=Range("N2:N" & lastrow)
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O" & lastrow)
Range("O2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Team Sports Pricelist").Select
Range("N5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Interface").Select
This is what your code might look like if you don't select anything. (Code isn't tested).
Dim Ws As Worksheet
Dim Rng As Range
Set Ws = Worksheets("List")
Set Rng = Ws.Range(Ws.Cells(2, "N"), Ws.Cells(Ws.Rows.Count, "N").End(xlUp))
Rng.Cells(1).AutoFill Destination:=Rng
Set Rng = Rng.Offset(0, 1)
Rng.Cells(1).AutoFill Destination:=Rng
Rng.Copy
Worksheets("Team Sports Pricelist").Range("N5").PasteSpecial xlPasteValues
I am having some issues with my VBA code that will select data from one sheet, copy it, paste it to a new sheet and insert a total at the bottom of the table. The first steps work, but I am struggling with the total , any help would be greatly appreciated. Here is what I have so far:
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "='Pricing Main'!RC[1]"
Range(xlToRight, xlDown).Offset(1, 0).Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=SUM(R[-36]C:R[-1]C)"
End Sub
If the problem is in making formula with dynamic range (while first row is always the same and the 2nd is different), you could use:
ActiveCell.FormulaR1C1 = "=SUM(R1C:R[-1]C)"
If you're using number without "[]" it will absolute address with $
In this example it will be ="SUM(A1:Ax)" where X is row before active cell
Another option is using Activecell.Formula and get this address by combining letters with Activecell.row
activecell.Formula = "=SUM(A1:A" & activecell.Row - 1 &")"
And after that you can autofill to other columns if needed by
activecell.AutoFill Destination:=range(activecell, activecell.Offset(0,5)), Type:=xlfilldefault
3rd option could be using range variables calculate address before formula:
Sub Makro1()
Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range
Set rng1 = Range("A1") Set rng2 = ActiveCell.Offset(-1, 0) Set rng3 = Range(rng1, rng2)
ActiveCell.Formula = "=SUM(" & rng3.Address & ")"
End Sub
If you have another problem, please, try explain it better ;)
Hey i just created a macro added headers deleted info and got data formatted
but i noticed that when i ran it for another file
it just deleted the data in that exact cell i now need to
do the same
but delete the row that the phrase sits on
imagine i had a cell a1 in other versions of the document that phrase could be in a2
my macro would only delete whats in A1
the phrase is ZFD
and whatever cell its in i need the macro to delete the entire row that phrase sits on
HELPPPPP
Sub UMR()
'
' UMR Macro
'
'
Range("A1").Select
ActiveCell.FormulaR1C1 = "Transaction_Type"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Meter_Point_Ref"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Actual_Read_Date"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Source"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Reason"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Meter_Serial_Number"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Meter_ROC_Count"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Meter_Read_Verified"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serialNumber"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serial_Number"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Corrector_Uncorrected_Reading"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Corrector_Corrected_Reading"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Corrector_ROC_Count"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Corrector_Usable_IND"
Range("O1").Select
ActiveCell.FormulaR1C1 = "Corrector_Read_Verified"
Range("A17").Select
Selection.ClearContents
Range("B17").Select
Selection.ClearContents
Columns("C:C").ColumnWidth = 8.29
Columns("C:C").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("E:E").Select
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
Columns("I:I").EntireColumn.AutoFit
Columns("J:J").EntireColumn.AutoFit
Range("Q1").Select
Columns("K:K").EntireColumn.AutoFit
Columns("L:L").EntireColumn.AutoFit
Range("R1").Select
Columns("M:M").EntireColumn.AutoFit
Columns("N:N").EntireColumn.AutoFit
Columns("O:O").EntireColumn.AutoFit
ActiveWindow.SmallScroll Down:=6
ActiveWindow.SmallScroll ToRight:=-9
ActiveWindow.SmallScroll Down:=-88
End Sub
As I just did have the time I reorganized your code a little. Be aware that this is not commonly done here on stackoverflow. For next time: At least try to code something, if it's wrong that's not a problem, that's where we can help. And for your information: I am quite the newby as well (3,5 months of vba so far), so it's not that hard. Even if my code is not perfected yet, most of the time I can get it to work somehow...
Try this once (read the comments in the code first):
Sub UMR()
Dim WS As worksheet
Set WS = AcitveWorkbook.ActiveWorksheet 'be aware this will always be run on the activesheet
Dim Values AS Variant
Values = Array("Transaction_Type", "Meter_Point_Ref", "Actual_Read_Date", "Meter_Reading_Source", "Meter_Reading_Reason", "Meter_Serial_Number", "Meter_Reading", "Meter_ROC_Count", "Meter_Read_Verified", "Corrector_serialNumber", "Corrector_serial_Number", "Corrector_Uncorrected_Reading", "Corrector_Corrected_Reading", "Corrector_ROC_Count", "Corrector_Usable_IND", "Corrector_Read_Verified")
Dim FindString As String
FindString = "ZFD"
Dim ZFDVal As Variant
Dim IRow As Integer
Dim ICol As Integer
Set ZFDVal = Ws.Find(What:=FindString, _
After:=Ws.Cells(Ws.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _ 'If the value is only a part of a cell it would be xlPart instead of xlWhole
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False) 'If you want it to Match the string exactly (regarding capital letters) you'll have to set this to true
IRow = Range(ZFDVal.Adress).Row 'This is untested...
For ICol = 1 To (UBound(Values)-LBound(Values))
Ws.Cells(IRow, ICol) = Values(ICol-1)
Next ICol
Range("A17").Clear ' I believe this was unintendet and just recorded alongside so you can delete these two rows...
Range("B17").Clear
Columns("A:O").EntireColumn.AutoFit
End Sub
If you get a run-time error please press "debug" and comment which line gets marked yellow. This way we can help you correcting the code...