Copy and Paste Row in Excel When Condition Met without overwriting - vba

I want to Copy and Paste Rows from one excel tab to another whenever a criteria is met. Currently, this is my code which I learnt from an online tutorial. Is there a way I can modify the code below to prevent overwriting and copying the same data twice whenever I click the button?
Private Sub CommandButton1_Click()
a = Worksheets("Results").Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To a
If Worksheets("Results").Cells(i, 29).Value = "Nutella" Then
Worksheets("Results").Rows(i).Copy
Worksheets("Nutella").Activate
b = Worksheets("Nutella").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Nutella").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Results").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Results").Cells(1, 1).Select
End Sub
Thanks

Related

How to copy the entire row of the active cell and paste it underneath with the formulas and data validation in shared workbook

I needed to copy the entire active cell row and automatically paste it underneath the active cell row including the conditional formatting and data validation. However everytime I try to share the workbook, the row is pasted before the active row without the conditional formatting and data validation.
Current vba
Sub AddRow ()
With ActiveCell
.EntireRow.Copy
.EntireRow.Insert shift = xlDown
End With
Application.CutCopyMode = False
End Sub
Try this
Sub AddRow()
With ActiveCell
.EntireRow.Copy
ActiveCell.Offset(1, 0).EntireRow.Insert (xlShiftDown)
End With
Application.CutCopyMode = False
End Sub

Copy and paste data and have it be updated automatically

So I created a copy and paste function. I had help previously with an error I encountered. However, I am now wanting to make the values copy and pasted to be updated when the original date is changed. So, my original thought was to paste something like =(ws.Cells(i, j). And have a nested for loop to with the values i staying the same as below and j going in between 6 and 16. But I couldn't get that to work.
If there is a special paste function or something that I am unaware of that would be great. Is there a way to get copy and paste data but also have it still be reliant on the original (updates when the original is changed).
If there is another question with a solution to this problem then I didn't see it and I am sorry.
I have my code below. And any help would be appreciated.
Private Sub CommandButton1_Click()
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Goals")
a = Worksheets("Goals").Cells(Rows.Count, 7).End(xlUp).Row
For i = 2 To a
If Worksheets("Goals").Cells(i, 20).Value = "Red" Then
ws.Activate
Set rng = ws.Range(ws.Cells(i, 6), ws.Cells(i, 16)) 'columns to be copied
rng.Copy
Worksheets("Scorecard").Activate
b = Worksheets("Scorecard").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Scorecard").Cells(b + 1, 2).Select
ActiveSheet.Paste
Worksheets("Goals").Activate
End If
Next
Application.CutCopyMode = False
Worksheets("Forms").Activate
Worksheets("Forms").Cells(22, 10).Select 'going back to the Forms page
End Sub
Try this
Worksheets("Goals").Range("I6:I16").Copy
Worksheets("Scorecard").Paste Link:=True
I hope you wont mind if the sheets switch in this process..
Thanks

VBA EXCEL REPLACE in database or dont copy if already there

First View (FIRST SHEET FOR UPLOADING)
Second View (DATABASE WHERE DATA IS PARSED AND MANUAL EDITION IS BLOCKED
SUB I MADE FOR DATA VALIDATION AND PARSING INFO
Hi fellow devs,
This is my first time using VBA. Im stucked atm because i need to add some functionality i cant figure out. I need to modify my sub so if by any chance there is a duplicate row on the "UPLOAD" slide it wont paste again in my "BASE" and if there is the same line but QTY modified it should update to new QTY. Could you please help me figure out how to change my sub?
Tks and regards.
EDIT:
This is the SUB:
Sub Button_Click()
ThisWorkbook.Worksheets("UPLOAD").Range("C4", Cells(Rows.Count, 3).End(xlUp)).Interior.ColorIndex = xlNone
For Each c In Worksheets("UPLOAD").Range("C4", Cells(Rows.Count, 3).End(xlUp))
If (Len(c) <> 12) And (c <> "") Then
c.Interior.ColorIndex = 3
MsgBox ("Faltan 12ncs!! Porfavor agreguelos o corrija los que estén mal.")
Exit Sub
End If
Next c
a = Worksheets("UPLOAD").Cells(Rows.Count, 1).End(xlUp).Row
For i = 4 To a
Worksheets("UPLOAD").Rows(i).Copy
Worksheets("BASE").Activate
b = Worksheets("BASE").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("BASE").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("UPLOAD").Activate
Next
Application.CutCopyMode = False
Worksheets("UPLOAD").Cells(1, 1).Select
End Sub
Before Loop
Create an array
Loop through your items
Inside the loop
Check if id is inside the array, skip item
Add id to the array
Upload item

Excel macro - check for a string in a cell, run a routine, move to the next row, do it again

I am not a Dev, but given I do use Excel, I have been tasked to create a looping macro that will check for a string ('Resource') in a cell and if it finds that string, then run a Copy and Paste code and then move to the next row. This starts at row 5 and runs continuously until row 199, but does not work on every row, hence the validation for the string Resource.
I have managed to create the macro for the Copy and Paste but it also has issues as I created it using the macro recorder and it only works on the row I actually did the recording on.
I am at a complete loss, can anyone help?
this is what I have so far
A New Resource name is added manually to the spreadsheet
the user clicks cell (C6) to focus the curser
the user clicks a macro button called 'Forecast for Future Project 1' to start the macro
On the button click the Macro will:
Interogate if cell to the left of current cell (B6) = 'Resource'
IF Yes, THEN
Sub CP()
DO
Range("C6").Select
Selection.Copy
Application.Goto Reference:="ProjAdd"
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=SUMIF('Current Project Utilisation'!R2C1:R62C1,RC1,'Current Project Utilisation'!R2C:R62C)+SUMIF('Future Project 1'!R2C1:R62C1,RC1,'Future Project 1'!R2C:R62C)"
Range("ProjAdd").Select
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range(Selection, Selection.End(xlToRight)).Select
ActiveSheet.Paste
Range("B6").Select
Loop Until ActiveCell.Address(0,0) = "$B$199"
End Sub
Move to cell under original active cell (C7) and Repeat the Macro until cell C199 is reached
If (B6) does not = 'Resource' then move to go to the cell under (C7) aand Repeat the Macro until cell C199 is reached
Refresh Worksheet to update data
Would something like this work for you?
Sub CopyPasteResource()
Dim CopyRange As Range
Dim Cell As Range
Set CopyRange = Workbooks("YourWorkBookName").Sheets("Sheet1").Range("C6:C199")
For Each Cell In CopyRange
If InStr(1, Cell.Offset(0, -1).Text, "Resource") Then
Cell.Copy
'paste where you wish
End If
Next Cell
End Sub
EDIT: Or do you want to loop through B6:B199 and then C6:199? I'm not entirely clear on the aim.
Ah the old macro recorder, generating 90% extra code since 1997. I couldn't exactly figure out from your question what exactly is being copied and to where but this code will loop through rows 5 to 199, check if the value in column B = "Resource" and then set the corresponding value in column C, you should be able to modify for your needs but I think you definitely want a structure more like this than what the recorder generated for you..
public sub cp()
Dim ws as Worksheet
Set ws = Worksheets("Current Project Utilisation")
Dim i as int
for iI = 5 to 199
if(ws.cells(i, 2).value = "Resource") then
ws.cells(i, 3).value = "what you're copying"
end if
next I
end sub
Assuming your cell range doesn't change you can do this for the looping part
Sub ResourceCheck()
Dim WS As Worksheet
Set WS = ActiveSheet
Dim Resources() As Long, r As Long
ReDim Resources(5 To 199)
For r = 5 To 199
If UCase(WS.Cells(r, 2).Value) = "RESOURCE" Then
WS.Cells(r, 3).Value = "x"
'Do copy paste part
End If
Next r
Application.Calculate
End Sub
Can you add a sample of your data? It's a bit hard to see what you're referencing to and how the data relates to each other.
Also, where is the "Projadd" cell reference? And what does it do?
Sub CP()
' I like to know what worksheet I'm on
Dim ws as Worksheet
' if it's a dedicated worksheet use this
' Set ws = ThisWorkbook.Worksheets("Sheet1")
' Otherwise following your current code
Set ws = ActiveSheet
' I also like to grab all my data at once
Dim Data as Variant
Data = ws.Range("B6:B199")
' No need to focus the cursor
For row = 5 to 199
' No need to select any range
' Is this case-sensitive???
If Data(row-4, 1) = "Resource" Then
' Copy C6??? Paste 'ProjAdd'
ws.Cells(row, 3).Copy Range("ProjAdd")
Application.CutCopyMode = False
End If
Next
End Sub

How to hide and copy a checkbox using vba

I have the base column with the checkbox, (the D column in my code), and i want to copy that column with the checbox to the other columns, but the column D must be hide(all the data including the checkbox).
the problem here is:
i don't know how to hide the checbox, when i hide the column the checbox still visible.
when i coppy the column the checbox in the colum does not be copied
This is the fuction that i actually used.
Private Sub cmdAddNewXref_Click()
Columns("D:D").Select
Selection.Copy
i = 3
Cells(2, i).Select
Do
i = i + 1
Loop While Cells(2, i) <> ""
Cells(2, i).Select
'MsgBox ActiveCell.Column
Columns(i - 1).Select
Columns("D:D").Select
Selection.Copy
Columns(i).Select
ActiveSheet.Paste
Selection.EntireColumn.Hidden = False
Application.CutCopyMode = False
Range("A1").Select
End Sub
but most importantly what i want to do, is possible?
EDIT 1: actually thanks to Scott Holtzman i can hide the checkbox with the columns.
Give this is a shot. There's probably a bit more of ideal way to do it, but I tested it and got it to work.
There are some assumptions on cell ranges and such that you will need to adjust to meet your exact spreadsheet specs.
Option Explicit
Private Sub cmdAddNewXref_Click()
Dim i As Integer
Dim ws As Worksheet
Set ws = Worksheets("mySheet") 'change as needed
'find next column to copy
i = 3
Do
i = i + 1
Loop While ws.Cells(2, i) <> ""
With ws.Columns("D:D")
.EntireColumn.Hidden = False
.Copy Columns(i)
End With
'copy checkbox in column D
Dim cb As Shape
Set cb = ws.Shapes("CheckBox1") 'change name as needed
cb.Copy
ws.Cells(4, i).Select 'assumes checkbox should be in row 4, change to wherever it is on column D for you
ws.Paste
ws.Columns("D:D").EntireColumn.Hidden = True
End Sub
CHange the paste, to paste special values, to hide the check its the .visible property I believe.
hope this helps.
From what I gathered... Copying the row, to somewhere else, but not the control, then he wanted to hide copied row. :)
Option Explicit
Sub test()
' From above code in yours I will be col number
Dim i As Integer
i = 5
Columns("A:A").Copy
Sheet2.Activate
ActiveSheet.Cells(1, i).PasteSpecial xlPasteValues
End Sub