Skip cell - Excel VBA Macro - vba

I am working on a VBA Macro for a budget sheet. When it copies data to cell B30 I want it to look at the cell and if there is data in the cell move to B31 and so on. However, when it gets to cell B41 I want it to throw up and error message. How would I go about writing this code?
Sheets("Budget with Revenue 2017-Test").Select
Range("B18:I18").Select
Application.CutCopyMode = False
Selection.Copy
Range("B22").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("Table2940[[Sum of Revenue]:[Sum of Savings]]").Select
Application.CutCopyMode = False
Selection.Copy
Range("B30").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("B21:I22").Select
Range("I21").Activate
Application.CutCopyMode = False
Selection.ClearContents
Range("B14:H14").Select
Range("H14").Activate
Selection.ClearContents
Range("J2:J3").Select
Selection.ClearContents
Range("G2").Select
Selection.ClearContents

Related

How to add a new row to another sheet without overwriting the row in VBA?

I have 2 sheets. I want to transfer a fixed range (a row of data) from sheet #1 (data sheet) to sheet #2 (info sheet) by clicking on a command button. The transferred data should be kept as records in sheet #2. But when I use the code below it overwrites the existing entry instead of adding a new record (row). How do I add a new row to sheet #2 and not overwrite the existing one?
Sheets("DATA").Select
Range("B85:G85").Select
Selection.copy
Sheets("INFO").Select
Range("B5").Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("DATA").Select
Range("B86:G86").Select
Application.CutCopyMode = False
Selection.copy
Sheets("INFO ").Select
Range("H5").Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Problem with the Code:
No calculation of Last Row
Excessive use of Select
Try this:
With Sheets("INFO")
Sheets("DATA").Range("B85:G85").Copy
.Range("B" & .Range("B5").End(xlDown).row + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("DATA").Range("B86:G86").Copy
.Range("H" & .Range("H5").End(xlDown).row + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With

Copying and Pasting into a new Table Row using VBA

I am trying to figure this out and I am hoping you can help
Basically I have Form and Data Sheet. I am looking to copy the information in the form into a new blank row within Table1 on the data sheet,
I have managed to get as far as the following but this causes the data to be over written each time, (rather than a a new row).
Sub Macro1()
Sheets("Form").Select
Range("G5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[ID]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Contact Date]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Channel]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Agent Name]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Contact ID]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("G3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Scored by]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Form").Select
Range("G4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("Table1[[#Headers],[Team Leader]]").Select
Selection.End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I realise this may seem like a simple question but I am struggling to work this out.
FYI - There will be 29 Columns to this table so If I should be doing something to make this cleaner, please let me know
Here's a more streamlined way to approach this:
EDIT - updated to add "config" array to reduce repetition
Sub Transfer()
Dim config, itm, arr
Dim rw As Range, listCols As ListColumns
Dim shtForm As Worksheet
Set shtForm = Worksheets("Form") '<< data source
With Sheets("Data").ListObjects("Table1")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
'array of strings with pairs of "[colname]<>[range address]"
config = Array("ID<>G5", "Contact Date<>D3", "Channel<>D4")
'loop over each item in the config array and transfer the value to the
' appropriate column
For Each itm In config
arr = Split(itm, "<>") ' split to colname and cell address
rw.Cells(listCols(arr(0)).Index).Value = shtForm.Range(arr(1)).Value
Next itm
End Sub
No copy/paste/select/activate required.

Excel Macro Possible Bug

I have made a Macro that performs some repetitive tasks (updates certain data connections and transfers this information to another Excel). The code works perfectly, however, sometimes, it changes the formatting of certain cells, in worksheets, that are not even called in the Macro.
This sounds as a bug. I have previously had the same problem, that was solved by removing the On Error Resume Next line.
Could you please read my code, and see if there is something that could be causing this bug? I really cannot allow to lose my formatting since I am using these sheets for important company reports.
Before running the Macro:
After running the Macro:
Here goes my code:
Sub TRANSFER_INPUT()
'
' TRANSFER_INPUT Macro
'
Dim MWWorkBook As Workbook
Set MWWorkBook = ActiveWorkbook
Sheets("PAR").Select
Dim Pateka As String
Worksheets("PAR").Activate
Pateka = Range("E5").Value
Dim Datum1 As String
Worksheets("PAR").Activate
Datum1 = Range("E6").Value
Dim InputExcel As Workbook
Workbooks.Open Filename:=Pateka & "INPUT" & Datum1 & ".xlsx", UpdateLinks:=3
Set InputExcel = ActiveWorkbook
'###
'MAIN WORKBOOK / PREFRLANJE FAJLOVI
'###
'INPUTBILANS
MWWorkBook.Activate
Sheets("INPUTBILANS").Select
Range("F11:M1000").Select
Selection.Copy
Range("Y11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'REFRESH BILANS VO INPUT
MWWorkBook.Activate
Sheets("INPUTBILANS").Select
Range("F10").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
'###
'###
'PREFRLI VO INPUT
'###
'###
'Kopiraj Bilans_1
Range("F11:V1000").Select
Selection.Copy
'Pastiraj Bilans_1 VO INPUT / Bilans_1 vo Bilans_2
InputExcel.Activate
Sheets("BILANS_1").Select
Range("B8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("B8:R1000").Select
Selection.Copy
Sheets("BILANS_2").Select
Range("B8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
''' PREFRLI I KOPIRAJ COSTS
MWWorkBook.Activate
Sheets("COSTS").Select
Range("A4").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
Range("A5:AV312").Select
Selection.Copy
InputExcel.Activate
Sheets("COSTS").Activate
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Kopiraj OPER.COST
MWWorkBook.Activate
Sheets("OPER.COST | NONOP | PRIHODI").Select
Range("D7").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
Range("G16:P63").Select
Selection.Copy
InputExcel.Activate
Sheets("OPER.COST").Select
Range("Z4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Kopiraj PRIHODI
MWWorkBook.Activate
Sheets("OPER.COST | NONOP | PRIHODI").Select
Range("D65:F204").Select
Selection.Copy
InputExcel.Activate
Sheets("PRIHODI").Select
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Kopiraj NONOP
MWWorkBook.Activate
Sheets("OPER.COST | NONOP | PRIHODI").Select
Range("F8:F14").Select
Selection.Copy
InputExcel.Activate
Sheets("NONOP").Select
Range("D5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Thank you so much!

Optimizing recorded macro; I know I need to write a loop, but am unsure how to do so

I have a block of code that goes like this:
Range("C3").Select
Application.CutCopyMode = False
Selection.Copy
Range("S3:BL3").Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Range("C4").Select
Application.CutCopyMode = False
Selection.Copy
Range("S4:BL4").Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
... and so on until row 257
The only part of the code that changes are the ranges I am selecting, so I know that I can write a loop to accomplish this without having to repeat myself on all of the other code, but I'm not really sure where to begin.
I want to do some counter like this, but I know I can't just put the variable "x" next to the column letter to have it repeat the process:
For x = 3 to 256
Range("Cx").Select
Application.CutCopyMode = False
Selection.Copy
Range("Sx:BLx").Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
To answer you conundrum about "I can't just put x", sure you can, with concatenation. Range("C" & x) would provide what you're looking for.
But, based on your given example, you seem to know the range you're wanting to iterate through; why not just select the range once, and paste the range once?
Range("C3:C256").Select
Application.CutCopyMode = False
Selection.Copy
Range("S3:BL256").Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False

VBA Loop to Copy Columns

I want to copy a defined number (lets say 10) of rows from one sheet ("Data") and paste it in another sheet ("Input). This will cause a bunch of stuff to calculate. Then I want to copy said calculated data (6 rows) from ("Input") to ("Data") and paste in a results table. THen I would repeat this a defined number of times for a certain number of columns (lets say 10).
I tried writing the code but it has literally been years since I have written code.
I used the Record Marco thing and got this:
Sub Macro2()
'
' Macro2 Macro
'
'
Range("C5:C14").Select
Selection.Copy
Sheets("Input").Select
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("P12:P19").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("C22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D5:D14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Input").Select
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("P12:P19").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data").Select
Range("D22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G16").Select
End Sub
I hope this makes sense
Sub Macro2()
Const NUM_TIMES As Long = 10
Dim shtInput As Worksheet, shtData As Worksheet
Dim rngCopy As Range, i As Long
Set shtInput = Sheets("Input")
Set shtData = Sheets("Data")
Set rngCopy = shtData.Range("C5:C15")
For i = 1 To NUM_TIMES
With shtInput
.Range("C5").Resize(rngCopy.Rows.Count, 1).Value = rngCopy.Value
.Calculate
rngCopy(1).Offset(17, 0).Resize(8, 1).Value = .Range("P12:P19").Value
End With
Set rngCopy = rngCopy.Offset(0, 1)
Next i
End Sub