Update Sheet but only Columns A:G - excel

I got help with the below code to get data from Columns A,C:G and J from Source Sheet and put it into Columns A to G in Target Sheet depending on 24th column(X) having a 1 in it in that row. It all works fine, my Target Sheet updates with the correct information from the Source Sheet, however it also deletes all data past Column G. For example if I type 100 in Column J in my Target Sheet when I come back to my Target Sheet next time it's gone. I want to keep the data I have in Columns past Column G.
How can i stop the code below from deleting my data in Columns after Column G? I tried a couple things but my excel goes into a fit as I think I'm creating a loop.
Any help is hugely appreciated.
Private Sub Worksheet_Activate()
Dim Source As Worksheet: Set Source = Sheets("From")
Dim Target As Worksheet: Set Target = Sheets("To")
Application.ScreenUpdating = False
Target.UsedRange.Offset(1).Clear
With Source.[A1].CurrentRegion
.AutoFilter 24, 1
Union(.Columns("A"), .Columns("C:G"), .Columns("J")).Offset(1).Copy
Target.Columns("A:G").End(3)(2).PasteSpecial xlValues
.AutoFilter
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

Related

Excel VBA - Insert a row below, copy the formulas and formats and insert specific default data in select columns

I have a considerable database in Excel. About half of the column data are entered by hand. One problem has plagued me for some time.
I insert rows by hand.
I copy the formulae by hand. Quite Error prone.
I set up the formats by hand.
The sheet is fairly complex.
I've tried to automate the process, alas always with some bug or another.
Sub InsertRow()
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
End Sub
This doesn't seem to copy the formulae. And how would I put default values in the specific columns?
All help is appreciated.
Here's a solution for your problem. Install the code in the code module of the worksheet on which you wish to insert rows. It's one of the pre-existing modules in the workbook, named after the tab. If you want the same action on several worksheets install a version of the procedure in each applicable code module.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const TriggerClm As String = "A" ' change to suit
Const FirstDataRow As Long = 2 ' change to suit
Dim Rng As Range
Set Rng = Cells(Rows.Count, TriggerClm).End(xlUp).Offset(1)
With Target
If (.Address = Rng.Address) And (.Row > FirstDataRow) Then
Rows(.Row - 1).Copy ' copies from last used row
' Rows(FirstDataRow).Copy ' copies from FirstDataRow
Rows(.Row).Insert Shift:=xlDown
On Error Resume Next
Rows(.Row - 1).SpecialCells(xlCellTypeConstants).ClearContents
Application.CutCopyMode = False
Cancel = True
End If
End With
End Sub
And this is what will happen. The code module captures when you double-click on a cell and the above procedure responds to that action. If the double-click was on the blank cell below the last used cell in column A (TriggerClm) a blank row will be inserted at that point. It will contain all the formats and formulas it copies from either the row above or the first row in the sheet (FirstDataRow).
And that also defines the required setup. You can specify a TriggerClm other that "A" and a FirstDataRow other than 2. And you must chose between the two sources to copy from, disabling the one you don't want to use. Please read the remarks in the code.
After many years of wavering I now favour the last used row over the first in most of my projects. I also usually add code to insert the date if one is required, usually in the TriggerClm.
You may be familiar with the original function of double-click to switch to in-cell editing. This functionality is cancelled when a row is inserted. But if you take out Cancel = True then the code would stop in Edit mode in the new cell.
Here's the code if you want to leave it as a macro in a module. Not that when excel copies "formulas" it also copies values. I also added a lien to show you how to change column 3 to 5
Sub InsertRow()
Dim CurRow As Range
Dim NewRow As Range
ActiveCell.EntireRow.Insert
Set CurRow = ActiveCell.EntireRow.Offset(1)
Set NewRow = ActiveCell.EntireRow
CurRow.Copy
NewRow.PasteSpecial Paste:=xlPasteFormulas
NewRow.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
NewRow.Columns(3) = 5
End Sub

Copy and Paste a Range of Cells to another sheet if a cell outside the range =Resolved then delete the row

I currently have multiple sheets for storing records of payments (things to be Pay and CantPay). I am trying to write a macro that will copy and paste Cells A:M on every row where column T = "Resolved" on the CantPay sheet (where the next empty row is the next row where "a" & row-number = blank) to the "Pay" sheet.
Within the sheet which i want to copy from there is data in columns A:T but N:T are not needed once the problem is resolved. So once i have copy and pasted the data within cells A:M i want to just delete the entire row. I have written some code from what i knew and looking online which isn't working. Any help would be much appreciated.
Thanks
I have tried recording a macro and writing my own but it seems the macro i have wrote is deleting row 1 which is where all my column headers are stored.
Sub MoveToPay()
Dim CantPay As Worksheet: Set CopySheet = Sheets("Can't Pay")
Dim ReadyToPay As Worksheet: Set PasteSheet = Sheets("£ Pay")
Dim lr As Long
Dim S As String
Application.ScreenUpdating = False
Columns(20).AutoFilter 1, "Resolved"
With Range("a2", Range("M" & Rows.Count).End(3))
.Copy PasteSheet.Cells(Rows.Count, 1).End(3).Offset(1)
.EntireRow.Delete
End With
Columns(20).AutoFilter
Application.ScreenUpdating = True
End Sub

Add new rows to existing table from external data with no duplicates

I have a spreadsheet with existing workers' names, IDs, and addresses.
These workers are hired and fired many times.
I pull a report (external data from a website) that lists these workers' names, IDs, and addresses that are currently employed.
Instead of adding and deleting the rows of these workers on my table(the table on the MASTER sheet) to match the report(the data on the EXTERNAL DATA sheet), I would like to add the rows of workers from the report(EXTERNAL DATA sheet) to my table(MASTER sheet) automatically without duplicates.
The reason I need to add the whole row is because I have more columns in the table(on the MASTER sheet) so I can add their job title, shift, and incentive pay. So I need to delete the whole row when I delete the worker...
I need some code to add rows from the external data table(EXTERNAL DATA sheet) on one sheet to my MASTER table on another sheet without duplicate workers.
This code works great on my sheet now. I might be able to adapt this code with some help...
`Private Sub Worksheet_Change(ByVal Target As Range)
' Code to move row from FIRED sheet to MASTER sheet when “REHIRED” is
'selected in column J
If Target.Column = 2 Then
' The line below is where I should change to something to compare if there
'is a new number
'in the "NUMBER" column of the "EXTERNAL DATA" sheet then move the row to
'the "MASTER" table.
If Target = "REHIRED" Then
Application.EnableEvents = False
nxtRow = Sheets("MASTER").Range("H" & Rows.Count).End(xlUp).Row + 1
Target.EntireRow.Copy _
Destination:=Sheets("MASTER").Range("A" & nxtRow)
Target.EntireRow.Delete
End If
End If
Application.EnableEvents = True
End If
End Sub`
I hope this makes sense.
Edit: updated/tested. Assumes both sets of data are formatted as Tables/ListObjects:
Sub Tester()
Dim lo As ListObject, loExt As ListObject, lr As ListRow
Dim rw As Range, shtExt As Worksheet, f As Range, shtMaster As Worksheet
Dim rwNew As Range
Set shtExt = Sheets("external data")
Set shtMaster = Sheets("master")
Set lo = shtMaster.ListObjects(1) 'or use the table name
Set loExt = shtExt.ListObjects(1) 'assumes the external data is a listobject
For Each lr In loExt.ListRows
'try to find the Id on the master sheet
Set f = lo.ListColumns(1).Range.Find(lr.Range(1).Value, lookat:=xlWhole)
If f Is Nothing Then
'no match: add a new row to the table
Set rwNew = shtMaster.ListObjects(1).ListRows.Add().Range
'populate the new row
rwNew.Cells(1).Value = lr.Range(1).Value
rwNew.Cells(2).Value = lr.Range(2).Value
rwNew.Cells(3).Value = lr.Range(3).Value
'etc transfer any other required values...
End If
Next lr
End Sub

Hide/Unhide cells with empty rows on Worksheet_Change

I have two columns of data that is pulled into a worksheet from data on other sheets elsewhere in the workbook via a formula in each cell...
The first column, Column A, has either a Yes, No or is blank from data that is pulled in via a formula from another sheet.
The second column, Column B, also has data pulled in from elsewhere but every row has data in it.
What I hope to do is hide any rows that does not have anything in column A. Any rows with data in column A should be visible. I'd like this to be updated via the worksheet_change event using VBA when data is entered that appears in column A.
Many thanks if you can help.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A:A")) Is Nothing Then Exit Sub
Application.EnableEvents = False
dim lrow as Integer
dim i as Integer
lrow = Cells(1, 2).End(xlDown).Row
For i = 1 To lrow
If Cells(i, 1) = 0 Then
Rows(i).Select
Selection.EntireRow.Hidden = True
End If
Next
Application.EnableEvents = True
End Sub
You have to insert this on the code of the sheet. right click the sheet name and press the view code and save it as macro enable.
It gets activated when changes have done to column a.

Excel Unhide Row based on value in a range

I know there are many questions here about hiding and unhiding rows in excel-vba but I can't seem to get my code to work...
I am working on a spreadsheet that has the month's totals listed on top. I want the sheet to hide the row of any month's total that doesn't appear in the sheet.
For ex. if there is no data in the list for January then the January totals row will be hidden. Once someone puts data for january in the list then the row will unhide.
I know that the script won't be running at all times so I added a "month reset" button that would run the script and hide/unhide all appropriate rows.
For some reason, every time I hit the button, the row just disappears regardless of whether or not the data is in the list below. I only wrote the code for January's row so far:
Private Sub CommandButton1_Click()
Sheets("Master Pipeline").Select
For Each cell In Range("B16:B100")
If cell.Value = "January" Then
Range("A3").EntireRow.Hidden = False
Else
Range("A3").EntireRow.Hidden = True
End If
Next
End Sub
Cells A3:A14 have the months listed with their totals in the cells next to them.
Rows 16:100 have the list of data, Column B has the month.
Please help...
Thanks in advance,
LT
You can use the following to hide any months that are missing in the data range. Note that you don't need to (and shouldn't) write code for each month. Also note that finding the value in the range is dramatically more efficient than looping through your data range.
Have your button call this sub:
Sub HideMissingMonths()
Dim ws As Worksheet
Dim months As Range
Dim data As Range
Dim cell As Range
Dim found As Range
Set ws = ThisWorkbook.Sheets("Master Pipeline")
Set months = ws.Range("A3:A14")
Set data = ws.Range("B16:B100")
Application.ScreenUpdating = False
' unhide all months before we begin
months.EntireRow.Hidden = False
For Each cell In months
Set found = data.Find(what:=cell, LookIn:=xlValues)
If found Is Nothing Then cell.EntireRow.Hidden = True
Next cell
Application.ScreenUpdating = True
End Sub

Resources