Copying multiple cells at once - excel

Could someone quickly tell me what's wrong with this syntax?
Sheets("Sheet1").Range("D" & i, "B" & i, "I" & i, "E" & i, "N" & i, "P" & i).Copy

This one is correct:
Sheets("Sheet1").Range("D" & i & ",B" & i & ",I" & i & ",E" & i & ",N" & i & ",P" & i).Copy
comma , should be inserted as string. I.e. you're trying to use (which is wrong):
Sheets("Sheet1").Range("D1","B1","I1","E1","N1","P1").Copy
but you should use:
Sheets("Sheet1").Range("D1,B1,I1,E1,N1,P1").Copy

Related

Why do I get Error 2042 in VBA Evaluate INDEX/MATCH?

I asked for advice everywhere but with no help so far!
= Evaluate("INDEX(TEMPDB!E" & lrCHIIMPORT2 + 1 & ":E" & lr3CHIMPORT2 & ",
MATCH(STATIONS!B" & RARTSTAT3 & "& STATIONS!" & matchARTIST & "1, TEMPDB!C" &
lrCHIIMPORT2 + 1 & ":C" & lr3CHIMPORT2 & " & TEMPDB!A" & lrCHIIMPORT2 + 1 &
":A" & lr3CHIMPORT2 & "))")
I get
Error 2042
Both criteria is STRING, output is INTEGER or STRING!
Sheets("STATIONS").Evaluate("INDEX(TEMPDB!E" & lrCHIIMPORT2 + 1 & ":E" &
lr3CHIMPORT2 & ", MATCH(STATIONS!B" & RARTSTAT3 & "& STATIONS!" & matchARTIST
& "1, TEMPDB!C" & lrCHIIMPORT2 + 1 & ":C" & lr3CHIMPORT2 & " & TEMPDB!A" &
lrCHIIMPORT2 + 1 & ":A" & lr3CHIMPORT2 & ",0))")
i can't believe... Just FALSE on the end...!

Issue when inserting a formula into a cell

I'm trying to insert formulas into cells using this following code:
ActiveCell.Formula = "=ISERROR(IF(O" & ActiveCell.Row & "=M; _
LEFT(O" & ActiveCell.Row & ";LEN(O" & ActiveCell.Row & ")-1)*1000000; _
LEFT(O" & ActiveCell.Row & ";LEN(O" & ActiveCell.Row & ")-1)*1000000000);" & vide & ")"
(vide is a string variable) but it keeps returning Application defined or Object defined error.
Thanks !
Change ; to ,
Change " & vide & " to """ & vide & """
Change M to ""M""
Change ISERROR to IFERROR
ActiveCell.Formula = "=IFERROR(IF(O" & ActiveCell.Row & "=""M"",LEFT(O" & ActiveCell.Row & ",LEN(O" & ActiveCell.Row & ")-1)*1000000,LEFT(O" & ActiveCell.Row & ",LEN(O" & ActiveCell.Row & ")-1)*1000000000),""" & vide & """)"
It's because of your local settings, you use ; unstead of ,.
If you must use ;, then try replacing:
ActiveCell.Formula
with:
ActiveCell.FormulaLocal
Also, I like to use Chr(34) to get the ".
So at the end change your formula to:
...1000000000);" & Chr(34) & vide & Chr(34) & ")"

Excel Formula in vba?

I am trying to insert the following formula using vba:
Cells(i, 17).Formula = "=IF(""" & Range("M" & i).value & """ = """","""",IFERROR(INDEX(Contacts!$D:$D,MATCH(""*"" & """ & Range("M" & i).value & """ & ""*"",Contacts!$C:$C,0)),"""")"
For some reason i get an application undefined error. Please can someone show me where i am going wrong?
You are missing a )
Cells(i, 17).Formula = "=IF(""" & Range("M" & i).value & """ = """","""",IFERROR(INDEX(Contacts!$D:$D,MATCH(""*"" & """ & Range("M" & i).value & """ & ""*"",Contacts!$C:$C,0)),""""))"

Shortening the IF And Formula using Date Function

Below is my IF AND formula using dates.
=if(and(C4>= & DATE(2016,01,01),C4<= &
DATE(2016,01,31)),"Jan",if(and(C4>= & DATE(2016,02,01),C4<= &
DATE(2016,02,29)),"Feb",if(and(C4>= & DATE(2016,03,01),C4<= &
DATE(2016,03,31)),"Mar",if(and(C4>= & DATE(2016,04,01),C4<= &
DATE(2016,04,30)),"Apr",if(and(C4>= & DATE(2016,05,01),C4<= &
DATE(2016,05,31)),"May",if(and(C4>= & DATE(2016,06,01),C4<= &
DATE(2016,06,31)),"Jun",if(and(C4>= & DATE(2016,07,01),C4<= &
DATE(2016,07,31)),"Jul",if(and(C4>= & DATE(2016,08,01),C4<= &
DATE(2016,08,31)),"Aug",if(and(C4>= & DATE(2016,09,01),C4<= &
DATE(2016,09,30)),"Sept",if(and(C4>= & DATE(2016,10,01),C4<= &
DATE(2016,10,31)),"Oct",if(and(C4>= & DATE(2016,11,01),C4<= &
DATE(2016,11,30)),"Nov",if(and(C4>= & DATE(2016,12,01),C4<= &
DATE(2016,12,31)),"Dec",0))))))
Please help me in shortening this formula.
Thanks.
Salman Khan
I hope, Below formula will resolve your problem
= TEXT(c4,"mmm")

Inserting semicolon in between data in a cell in Excel

I have data in column A, C, E and G. I want column I to hold all of the data from these separated by semicolons, I have searched the web but all I find is how to replace line breaks with semicolons and that doesn't do it. Below are the 3 pieces of code I have attempted to work into it
Range("I" & i).Value = "=A" & i & Chr(59) & "&" & "C" & i & "&" & "E" & i & "&" & "G" & i
Range("I" & i).Value = "=A" & i & "&C" & i & "&E" & i & "&G" & i
Range("I" & i).Value = Range("A" & i).Value + ; + Range("C" & i).Value + ; + Range("E" & i).Value + ; + Range("G" & i).Value
The second line comes closest to what I want but when I add & ";" into it I get errors.
Any ideas thoroughly appreciated
SOLVED : This was solved by an answer below, I had managed my own fix which I will detail but the accepted answer is a cleaner, more efficient way
mystring = "=A" & i & "&" & Chr(34) & ";" & Chr(34) & "& C" & i & "&" & Chr(34) & ";" & Chr(34) & "& E" & i & "&" & Chr(34) & ";" & Chr(34) & "& G" & i
Range("I" & i).Value = mystring
You should use "&"";""".
In detail, you should use something like
"=A" & i & "&"";""" & "&C" & i
which result in
=A1&";"&C1
suppose i = 1.
=CONCATENATE(A1, ";", C1, ";", E1, ";", G1)
Using VBA
Range("A2").FORMULA = REPLACE("=CONCATENATE(A1, ';', C1, ';', E1, ';', G1)", "'", CHR(34))

Resources