MS Excel VBA - Copying SpecialCells while keeping the destination formatting - vba

I would like to copy these special cells to a destination, while keeping the destination's formatting. I'm thinking I have to put .values somewhere in the lines. The code is as follows:
GRBReportSheet.Range("C" & GRBReportSheetFR & ":E" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy Destination:=GRB.Range("C30")
GRBReportSheet.Range("F" & GRBReportSheetFR & ":J" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy Destination:=GRB.Range("H30")
Thank you in advance for your time!

Try this.
GRBReportSheet.Range("C" & GRBReportSheetFR & ":E" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy
GRB.Range("C30").PasteSpecial xlValues
GRBReportSheet.Range("F" & GRBReportSheetFR & ":J" & GRBReportSheetLR).SpecialCells(xlCellTypeVisible).Copy
GRB.Range("H30").PasteSpecial xlValues

Related

VBA Run-time Error 1004 for Special Characters in Soft-coded Vlookup Function

When I was trying to throw a vertical bar "|" into my vlookup function, I got a 1004 error. I am not sure where it came from.
Initially the data table looks like this:
And my code was like this:
Sheets("Sheet 1").Cells(i, 2).Formula = "=VLOOKUP($A" & i & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Everything worked fine till the format of the data table got changed to:
So I had to throw the vertical bar "|" into my code. My new code was like this:
NewKey = Sheets("Sheet 1").Cells(i, 1).Value & "|" & Sheets("Data").Cells(i, 2).Value
Sheets("Sheet 1").Cells(i, 3).Formula = "=VLOOKUP(" & NewKey & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Apparently Excel 2010 did not like my codes and threw me a 1004 error. I am wondering if anyone knows a solution to this. Thank you in advance for your help.
NewKey needs to be in quotes when the formula is placed on the sheet, so you need to include the quotes in the string:
NewKey = Sheets("Sheet1").Cells(i, 1).Value & "|" & Sheets("Sheet1").Cells(i, 2).Value
Sheets("Sheet1").Cells(i, 3).Formula = "=VLOOKUP(""" & NewKey & """,Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"

Vlookup with Integer Variables

I am trying to do a Vlookup in my code and I have few integer variables. It gives me the error:
Run-time Error 1004
The code stops in the line of vlookup. All the variables have values. If someone can see where the problem, please let me know.
'Employees
empWS.Range("D1:G" & empLR).Copy
With tmpWB.Worksheets(2)
.Cells(1, 1).PasteSpecial xlPasteValues
.Range("$A$1:$D$" & empLR).AutoFilter Field:=1, Criteria1:=1
.Range("A2:D" & empLR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.ShowAllData
empLR = .Cells(Rows.Count, "A").End(xlUp).Row
.Columns(1).Delete
.Range("D1").Value = "start date"
.Range("D2:D" & empLR).FormulaR1C1 = "=VLOOKUP(RC[-3],SAP!R1C1:R " & SLRow & " C70, StartColumn,0)" 'it stops here
.Columns("A:D").AutoFit
End With
"=VLOOKUP(RC[-3],SAP!R1C1:R " & SLRow & " C70," & StartColumn & ",0)"
You need to put startcolumn outside the speech marks
EDIT
"=VLOOKUP(RC[-3],SAP!R1C1:R" & SLRow & " C70," & StartColumn & ",0)"
(Patrick Honorez spotted that there was an extra space after R in the first bit)
Try removing extra spaces:
.Range("D2:D" & empLR).FormulaR1C1 = "=VLOOKUP(RC[-3],SAP!R1C1:R" & SLRow & "C70, StartColumn,0)"
Space in a range address is an intersection operator.

Is there a Cells().Formula function?

my script compiled fine so I assumed it was ok but couldn't find it and of course it error's out on this line, this is encapsolated in a while loop, I'm just trying to have a simple way to add a formula to a row of data, then do something similar to add it to a row of data. Is there no .Formula or do I have another error?
Cells(arow, acol).Formula = "=COUNTIF(" & wsData.Name & "!" & Cells(fdRow, acol).Address & ":" & Cells(ldRow, acol) & ")"
acol = acol + 1
Just a few little changes needed:
You're missing a .Address on Cells(ldRow, acol)
I also included apostrophes ' around the worksheet name (only necessary if the name has spaces in it).
As #Wayne G. Dunn also pointed out you need the second parameter of the COUNTIF function, which I have put at the end as 1. You'll need to update that.
Here is the updated code:
Cells(arow, acol).Formula = "=COUNTIF('" & wsData.Name & "'!" & Cells(fdRow, acol).Address & ":" & Cells(ldRow, acol).Address & ",1)"

Using variable worksheet as formula destination in vba

I am writing a fairly complicated macro, but the problem I am having is creating a formula on one sheet, in the code "Input_Sheet", to equate itself to a cell in a newly created worksheet, variably set as "ws". Each iteration of ws is named, so that's not an issue. I figured the correct way to do it was:
ActiveCell.FormulaR1C1 = " & ws.name & !R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"
(Don't worry about the totalRowCounter & totalColumnCounter variables, they are defined appropriately). I just don't know why the formula isn't appropriately referencing the new ws sheet. Any thoughts?
You just need to take your ws.name out of the quotes, also adding an apostrophe before and after the sheet name will help with any sheets that may have a space in the name:
ActiveCell.FormulaR1C1 = "='" & ws.name & "'!R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"
I think it would be the below:
ActiveCell.FormulaR1C1 = "=" & ws.name & "!R[" & totalRowCounter & "]C[" & totalColumnCounter & "]"

Insert formula with reference to active cell row

I am trying to insert a formula into a cell and trying to use a dynamic cell reference by using ActiveCell.Row.
Range("Q" & ActiveCell.Row).Formula = "=IF(Range(""P"" & ActiveCell.Row).Address ="""",""DD/MM/YYY"",CONCATENATE(NETWORKDAYS(O21,P21),"" Working Days""))"
But I am getting an application defined error.
Please can someone show me where I am going wrong? Thanks
Range("Q" & ActiveCell.Row).Formula = "=IF(" & Range("P" & ActiveCell.Row).Address & " ="""",""DD/MM/YYY"",CONCATENATE(NETWORKDAYS(O21,P21),"" Working Days""))"
You have to comment out the VBA code in the string.