Image
Edit: to help clarify, I'd like to be able to populate B2:B6 through VBA so I can copy paste section A2:B6 down. My problem is that next month I will lose the August section and only have Sep to Dec, and so on as the year goes on.
This is my first time actually asking a question here so sorry in advance if I do something incorrectly. I'm very new to vba and need help getting this code to adjust itself and know when to stop.
My old code is this:
ActiveCell.FormulaR1C1 = "=RC[1]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-1]C[2]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-2]C[3]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-3]C[4]"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-4]C[5]"
ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-5]C[6]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-6]C[7]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-7]C[8]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-8]C[9]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-9]C[10]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-10]C[11]"
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveCell.FormulaR1C1 = "=R[-11]C[12]"
'ActiveCell.Offset(1, 0).Range("A1").Select
Where all it does is transpose a year's worth of data into a singular column. I'm trying to end with something like:
If ActiveCell.Offset(0, 1).Value <> "Dec" Then
c As Long
For c = 1 To 12
ActiveCell.FormulaR1C1 = "=RC[&c&]"
ActiveCell.Offset(1, 0).Range("a1").Select
Next c
Where it will adjust the C# and stop after it reaches a certain value in the next column. Currently I just add or remove a ' in front of each pair of the old code to get it to stop where i need it to but i'd like it to be able to do it by itself.
Thanks!
Try this:
Dim rng As Range
Dim last_col As Integer
last_col = Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column
If last_col > ActiveCell.Column Then
Set rng = Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(ActiveCell.Row, last_col))
rng.Copy
ActiveCell.Offset(1, 0).PasteSpecial xlPasteAll, Transpose:=True
Set rng = Nothing
End If
Related
I try
Do
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=R[-1]C+0.01"
Loop Until ActiveCell.Value = "$C$1"
But it's not working
Pls. help
i try to add + 0.01 from first value (1) until equal last value (1.9)
Here is my all code
[Sub ExtractRC()
Range("A2:A" & Range("A2").End(xlDown).Row).Select
Cells(Rows.Count, "A").End(xlUp).Select
Selection.Copy
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Range("k2").Select
ActiveCell.FormulaR1C1 = Range("A2").Value
Do
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=R\[-1\]C+0.01"
Loop Until ActiveCell.Value = "$C$1"
End Sub][1]
P.S. Value in Column A can change
I receive data that is similar in content, yet varies in the number and order of columns. I installed a drop down permanently in A6, copying it to each column in row 6,of the other columns, then select the appropriate header from the list. How can I amend my macro so it would either copy the DV from A6 or create identical headers where required? (determined by countA in Row 5)
This VBA solution places text where I want the dropdowns. Please tell me what I should use to replace the text "same dropdown as A6" so that it will automatically insert a dropdown with the header choices.
Private Sub CmdSubmit_Click()
Dim i As Integer
For i = 1 To 50
ActiveSheet.Select
Range("A5").Select
If ActiveCell.Offset(0, 1).Value >= "1" Then
ActiveCell.Offset(1, 0).Select
Else
Selection.End(xlToLeft).Offset(0, 1).Select
End If
ActiveCell.Offset(0, 1).Value = "same drop down as A6"
ActiveCell.Offset(0, 2).Value = "same drop down as A6"
ActiveCell.Offset(0, 3).Value = "same drop down as A6"
ActiveCell.Offset(0, 4).Value = "Same drop down as A6"
Next i
End Sub
This works, but it is not dynamic: Can we make it dynamic?
Sub Thiscopypaste()
Dim rngcopy As Range
Dim i As Integer
Set rngcopy = ActiveSheet.Range("A6")
rngcopy.Copy
Range("B5").Select
If ActiveCell.Value >= "1" Then
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Range("C5").Select
If ActiveCell.Value >= "1" Then
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Range("D5").Select
If ActiveCell.Value >= "1" Then
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Range("E5").Select
If ActiveCell.Value >= "1" Then
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Range("F5").Select
If ActiveCell.Value >= "1" Then
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
End Sub
If the dropdown you speak of is a data validation list then you need to perform the following:
Private Sub CmdSubmit_Click()
Dim i As Integer
Dim rngCopy As Range
For i = 1 To 50
'ActiveSheet.Select
Set rngCopy = ActiveSheet.Range("A6")
rngCopy.Copy
If rngCopy.Offset(-1, i).Value >= 1 Then
'ActiveCell.Offset(1, 0).Select
rngCopy.Offset(0, i).PasteSpecial xlPasteAll
Else
Set rngCopy = rngCopy.End(xlToLeft).Offset(0, i)
End If
'rngCopy.Offset(0, i).PasteSpecial xlPasteAll
'rngCopy.Offset(0, 2).PasteSpecial xlPasteAll
'rngCopy.Offset(0, 3).PasteSpecial xlPasteAll
'rngCopy.Offset(0, 4).PasteSpecial xlPasteAll
Next i
Set rngCopy = Nothing
End Sub
I'm looking for a VBA code that will look in the 2nd row for specific dates.
Basically, I'm writing a "Begin" date (ex: 2018-07-01) and an "End" date (ex: 2018-07-31) and I want my code to look for everything in between, including those dates, and copy paste all the information in another excel sheet. What the function will do at the end is it'll look into many sheets for those dates and copy every bit of information that are below those dates, and paste them all into one main sheet.
What I'm attaching below is what I've done so far:
Sub Copie()
Sheets("1").Select
Range("B1:H1").Select
Selection.Copy
Sheets("Per Employe").Select
Range("A4").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J1:P1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Cells(1, 1).Select
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Per Employe").Select
Cells(4, 1).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("1").Select
Range("B2:H2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J2:P2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
End Sub
What I'm trying to add is the function to look up the dates and only copy/paste the dates that were asked for in the main excel sheet, which would be "Per Employe". Would anyone have any solution to this? The cells that contain the begin and end date would be D1 & F1.
I have this macro (written in Excel 2016/Windows) that acts a very simple reservation tool, checking if an asset is currently booked or free. Depending on this, it either writes when the booked period will end or when the next booked period will start in another worksheet:
Sub Schaltfläche1_Klicken()
Worksheets("Tabelle10").Activate
With Columns(4)
.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues).Activate
End With
ActiveCell.Offset(0, -3).Select
If Selection.Value = "TODAY AM" Then
Sheets("HTML Output").Range("B3").Value = "Desk booked from this afternoon. Next availability"
ActiveCell.Offset(0, 3).Select
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Select
ActiveCell.Offset(0, -2).Select
Selection.Copy
Sheets("HTML Output").Range("C3").PasteSpecial xlPasteValues
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("HTML Output").Range("D3").PasteSpecial xlPasteValues
ElseIf Selection.Value = "TODAY PM" Then
Sheets("HTML Output").Range("B3").Value = "Desk booked from this afternoon. Next availability"
ActiveCell.Offset(0, 3).Select
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Select
ActiveCell.Offset(0, -2).Select
Selection.Copy
Sheets("HTML Output").Range("C3").PasteSpecial xlPasteValues
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("HTML Output").Range("D3").PasteSpecial xlPasteValues
ElseIf Selection.Value = "TOMORROW AM" Or Selection.Value = "TOMORROW PM" Or Selection.Value = "FUTURE" Then
Sheets("HTML Output").Range("B3").Value = "Desk free until (including)"
ActiveCell.Offset(-1, 1).Select
Selection.Copy
Sheets("HTML Output").Range("C3").PasteSpecial xlPasteValues
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("HTML Output").Range("D3").PasteSpecial xlPasteValues
End If
End Sub
This works perfectly fine in Office 2016 (Windows 10) but results in a Compile error: Expected Function or variable when I try to run it in Office 2011 for Mac or Office 2015 for Mac.
Can anyone point me in the right directions as for the reason(s) for this or tell me how to change the code to make it work?
Thanks in advance!
Jascha
The error handling in VBA Excel 2011 is not as great as it's Window's counterpart.
You were getting that error because you were using With/End With with ActiveCell.Offset(0, 1).Select
The best way to reproduce that error is paste this code in a module
Sub Schaltfläche1_Klicken()
With ActiveCell.Offset(0, 1).Select
End With
End Sub
Note: You and I didn't get that error later because you modified your post which we both tested :)
Interesting Read
I have a spreadsheet that pulls certain items out of a database by an autofilter macro and puts them into different sections. I have formulas that go in and are autofilled down to every line in each section. The problem I am running into is if a section only has one line my macro will debug. Below is my code that inserts the formulas and autofills them down. The very last row is the autofill macro and the one I need help with. Can someone please provide me an override that says if there is no lines to autofill to just move on to the next step. I'm not sure how this code would go. Thanks
'To insert formulas
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IF(RC5<'Data Entry'!R2C2,""*"","""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IF(RC18=TRUE,IFERROR(VLOOKUP(RC2,Database!C[-2]:C[9],11,FALSE),""""),0)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC2,Database!C[-3]:C[8],10,FALSE),"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR((VLOOKUP(RC9,Pull!C1:C5,4,FALSE))*RC4,"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR((VLOOKUP(RC9,Pull!C1:C5,5,FALSE))*RC4,"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(SUM(RC4,RC6:RC7),"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC2,Database!C[-7]:C[4],6,FALSE),"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IF(RC18=TRUE,IFERROR(VLOOKUP(RC9,'Pull'!C1:C5,2,FALSE),""""),"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(RC8*RC10,"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(RC8+RC11,"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(RC16*R9C13,"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IF(RC18=TRUE,IFERROR(VLOOKUP(RC9,'Pull'!C1:C5,3,FALSE),""""),"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(RC16*RC14,"""")"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IFERROR(RC12/(1-R9C13-RC14),"""")"
Range(Cells(Selection.Row, 3), Cells(Selection.Row, 17)).AutoFill Destination:=Range(Cells(Selection.Row, 3), "Q" & Range("B" & Rows.Count).End(xlUp).Row)
I'd set a LastRow variable, calculated the way you already do, and test whether it's greater than the Selection row:
Dim LastRow as Long
LastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
...
If LastRow > Selection.Row Then
Range(Cells(Selection.Row, 3), Cells(Selection.Row, 17)).AutoFill Destination:=Range(Cells(Selection.Row, 3), "Q" & LastRow)
EndIf
By the way, if you search on "VBA avoid Select statements" you'll get some info on why that's a good idea and how to do it. In this case I'd set a CellWithFormula variable at the beginning of the code:
Dim CellWithFormula as Excel.Range
Set CellWithFormula = Activcell
CellWithFormula.FormulaR1C1 = "=IF(RC5<'Data Entry'!R2C2,""*"","""")"
Set CellWithFormula = CellWithFormula.Offset(0, 1)
... and so on.