I am trying to clear all of the contents from columns E through AN for only the rows that have data in Column E (and not continue through the rest of the sheet). I would like the formulas in the cells to stay intact.
Any help greatly appreciated!
Sub ClearJEdetails()
Dim rng As Range
Set rng = Range("e12:AI1048530")
'Selecting only hardcoded data
rng.SpecialCells(xlCellTypeConstants).Select
Selection.ClearContents
End Sub
Sub ClearJEformulas()
Dim rng As Range
Set rng = Range("aj13:Ak1048530")
'Selecting only formulas
rng.SpecialCells(xlCellTypeFormulas).Select
Selection.ClearContents
End Sub
Clear Contents of Filtered Rows in Certain Columns
Adjust the values in the constants section.
Option Explicit
Sub DeleteFilteredEntireRows()
Const wsName As String = "Sheet1"
Const hRow As Long = 1
Const lrCol As String = "E"
Const Cols As String = "E:AN"
Const Criteria As String = "<>" ' non-blanks
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
If ws.AutoFilterMode Then ws.AutoFilterMode = False
Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, lrCol).End(xlUp).Row
Dim rCount As Long: rCount = lRow - hRow + 1
Dim scrg As Range: Set scrg = ws.Cells(hRow, lrCol).Resize(rCount)
Dim scdrg As Range: Set scdrg = scrg.Resize(rCount - 1).Offset(1)
scrg.AutoFilter 1, Criteria
Dim vcdrg As Range
On Error Resume Next
Set vcdrg = scdrg.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
ws.AutoFilterMode = False
If vcdrg Is Nothing Then Exit Sub
Dim drg As Range: Set drg = Intersect(vcdrg.EntireRow, ws.Columns(Cols))
drg.ClearContents
End Sub
Related
I am trying to extract data from different sheets in a summary sheet.
The referencing does not work.
Sub Summary_LPI()
Dim wkSht As Worksheet, wsC As Worksheet, rngSearch As Range
Dim shNCell As Range
Set wsC = Sheets("Summary")
Set rngSearch = wsC.Range("A2:A60")
For Each wkSht In ThisWorkbook.Worksheets
'find the sheet name cell in rngSearch:
Set shNCell = rngSearch.Find(what:=wkSht.Name, LookIn:=xlValues, Lookat:=xlWhole,
MatchCase:=False)
'if found:
If Not shNCell Is Nothing Then
'copy the below built array in the necessary place
wkSht.Range("AZ56").Value = wsC.Range(shNCell.Offset(0, 6), shNCell.Offset(1, 6)).Value
End If
Next wkSht
End Sub
Copy Data Into a Summary Worksheet
Adjust the values in the constants section.
The order of the columns in the Summary worksheet needs to be the same as in each individual worksheet.
The number of columns to be pulled is defined by the last non-empty column in the first (header) row of the Summary worksheet.
Option Explicit
Sub Summary_LPI()
' s - Source, d - Destination
Const sfvCol As String = "AY" ' First Value Column
Const dName As String = "Summary"
Const dlCol As String = "A" ' Lookup Column
Const dfvColString As String = "F" ' First Value Column
Const dhRow As Long = 1 ' Header Row
Dim wb As Workbook: Set wb = ThisWorkbook
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dfRow As Long: dfRow = dhRow + 1 ' First Row
Dim dlrow As Long ' Last Row
dlrow = dws.Cells(dws.Rows.Count, dlCol).End(xlUp).Row
If dlrow < dfRow Then Exit Sub ' no data
Dim dlcrg As Range ' Lookup Column Range
Set dlcrg = dws.Range(dws.Cells(dfRow, dlCol), dws.Cells(dlrow, dlCol))
Dim dfvCol As Long: dfvCol = dws.Columns(dfvColString).Column
Dim dlvCol As Long ' Last Value Column
dlvCol = dws.Cells(dhRow, dws.Columns.Count).End(xlToLeft).Column
If dlvCol < dfvCol Then Exit Sub ' no data
Dim vcCount As Long: vcCount = dlvCol - dfvCol + 1 ' Value Columns Count
Application.ScreenUpdating = False
Dim sws As Worksheet
Dim svrrg As Range ' Value Row Range
Dim svRow As Long ' Value Row
Dim dvrrg As Range ' Value Row Range
Dim dlCell As Range ' Lookup Cell
For Each dlCell In dlcrg.Cells
Set dvrrg = dlCell.EntireRow.Columns(dfvCol).Resize(, vcCount)
On Error Resume Next
Set sws = wb.Worksheets(CStr(dlCell.Value))
On Error GoTo 0
If sws Is Nothing Then ' worksheet doesn't exist
dvrrg.ClearContents ' remove if you want to keep the previous
Else ' worksheet exists
svRow = sws.Cells(sws.Rows.Count, sfvCol).End(xlUp).Row
Set svrrg = sws.Cells(svRow, sfvCol).Resize(, vcCount)
dvrrg.Value = svrrg.Value
Set sws = Nothing
End If
Next dlCell
Application.ScreenUpdating = True
MsgBox "Summary updated."
End Sub
I am looking to filter out a set of data with the criteria being if column A has over 5 characters in the string delete it.
However, before I delete it, I want to copy these entries to a sheet named "fixed"
The code I have at the moment works for the first entry, but doesn't loop through and I am unsure how to fix that...
Code:
Dim LR As Long, i As Long
LR = Worksheets("Output Sheet").Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
If Len(Range("A" & i).Value) >= 5 Then
Rows(i).EntireRow.Cut Worksheets("Fixed").Range("A:D")
Rows(i).Delete
End If
Next i
The data it is copying has 4 columns if that's of any help? I just can't seem to figure out why it doens't look but I am nearly positive it's a simple fix so any pointers would be appreciated.
Dim f As Long
Set Rng = Worksheets("Black List").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Application.ScreenUpdating = False
With Worksheets("Output Sheet")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For f = Lastrow To 1 Step -1
If Not IsError(Application.Match(.Range("A" & f).Value, Rng, 0)) Then
.Rows(f).Delete
End If
Next f
End With
Application.ScreenUpdating = True
Backup Data
This will add a formula (=LEN(A1)) to an inserted column range (E), to calculate the length of the values of the criteria column (A), and filter this range.
The filtered data (sdvrg) will be copied (appended) to another worksheet (Fixed) and the filtered data's entire rows will be deleted.
Finally, the inserted column (E) will be deleted.
Option Explicit
Sub BackupData()
Const sName As String = "Output Sheet"
Const sCols As String = "A:D"
Const scCol As Long = 1 ' Criteria Column
Const shRow As Long = 1 ' Header Row
Const sLenCriteria As String = ">5"
Const dName As String = "Fixed"
Const dCol As String = "A"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
If sws.AutoFilterMode Then sws.AutoFilterMode = False
Dim slRow As Long
With sws.Columns(sCols).Columns(scCol)
slRow = .Cells(.Cells.Count).End(xlUp).Row
End With
If slRow <= shRow Then Exit Sub ' no data or just headers
Dim srCount As Long: srCount = slRow - shRow + 1
' Source Table Range ('strg') (headers)
Dim strg As Range: Set strg = sws.Rows(shRow).Columns(sCols).Resize(srCount)
' Source Data Range ('sdrg') (no headers)
Dim sdrg As Range: Set sdrg = strg.Resize(srCount - 1).Offset(1)
Dim scCount As Long: scCount = strg.Columns.Count
Application.ScreenUpdating = False
' Source Inserted Column Range ('sicrg') (headers)
Dim sicrg As Range: Set sicrg = strg.Columns(1).Offset(, scCount)
sicrg.Insert Shift:=xlShiftToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Set sicrg = sicrg.Offset(, -1) ' account for 'Insert'
' The formula is also written to the header row which is irrelevant
' to the upcoming 'AutoFilter'.
sicrg.Formula = "=LEN(" & strg.Cells(1, scCol).Address(0, 0) & ")"
sicrg.AutoFilter 1, sLenCriteria
' Source Data Visible Range ('sdvrg') (no headers)
Dim sdvrg As Range
On Error Resume Next ' prevent 'No cells found' error.
Set sdvrg = sdrg.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
sws.AutoFilterMode = False
Dim WasBackedUp As Boolean
If Not sdvrg Is Nothing Then
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
If dws.AutoFilterMode Then dws.AutoFilterMode = False
Dim dfCell As Range
Set dfCell = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Offset(1)
sdvrg.Copy dfCell
sdvrg.EntireRow.Delete Shift:=xlShiftUp ' resizes 'sicrg' appropriately
WasBackedUp = True
End If
sicrg.Delete Shift:=xlShiftToLeft
Application.ScreenUpdating = True
If WasBackedUp Then
MsgBox "Data backed up.", vbInformation
Else
MsgBox "No action taken.", vbExclamation
End If
End Sub
I have two sheets in my excel file:
Input Sheet: Sheet1
Target Sheet: Sheet2
What I want to achieve is to paste the value start from the column that I defined in cell C5 and also start from the row that I defined in cell C6. If the range defined by cell C5 and C6 already have data, then it will find the next empty row based on the column in cell C5 and paste the data in that empty row.
For example in the screenshot above, the starting column & row defined in cell C5 & C6 is B8, so the copied value will be pasted starting from cell B8 until E8. However, if the row already have data, then it will find the next empty row based on column B (which is B9) and paste it there.
I'm not sure how to modified my current script:
Public Sub CopyData()
Dim InputSheet As Worksheet ' set data input sheet
Set InputSheet = ThisWorkbook.Worksheets("Sheet1")
Dim InputRange As Range ' define input range
Set InputRange = InputSheet.Range("G6:J106")
Dim TargetSheet As Worksheet
Set TargetSheet = ThisWorkbook.Worksheets("Sheet2")
Const TargetStartCol As Long = 2 ' start pasting in this column in target sheet
Const PrimaryKeyCol As Long = 1 ' this is the unique primary key in the input range (means first column of B6:G6 is primary key)
Dim InsertRow As Long
InsertRow = TargetSheet.Cells(TargetSheet.Rows.Count, TargetStartCol + PrimaryKeyCol - 1).End(xlUp).Row + 1
' copy values to target row
TargetSheet.Cells(InsertRow, TargetStartCol).Resize(ColumnSize:=InputRange.Columns.Count).Value = InputRange.Value
End Sub
Any help or advice will be greatly appreciated!
Testing Scenario 1
Output of Testing Scenario 1
Please, try the next code:
Public Sub CopyData_()
Dim InputSheet As Worksheet: Set InputSheet = ThisWorkbook.Worksheets("Sheet1")
Dim InputRange As Range: Set InputRange = InputSheet.Range("G6:J106")
Dim arr: arr = InputRange.Value
Dim TargetSheet As Worksheet: Set TargetSheet = ThisWorkbook.Worksheets("Sheet2")
Dim TargetStartCol As String, PrimaryKeyRow As Long
TargetStartCol = TargetSheet.Range("C5").Value ' start pasting in this column in target sheet
PrimaryKeyRow = TargetSheet.Range("C6").Value ' this is the row after the result to be copied
Dim InsertRow As Long
InsertRow = TargetSheet.cells(TargetSheet.rows.Count, TargetStartCol).End(xlUp).row + 1
If InsertRow < PrimaryKeyRow Then InsertRow = PrimaryKeyRow + 1 'in case of no entry after PrimaryKeyRow (neither the label you show: "Row")
' copy values to target row
TargetSheet.cells(InsertRow, TargetStartCol).Resize(UBound(arr), UBound(arr, 2)).Value = arr
End Sub
Not tested, but if should work, I think. If something not clear or going wrong, please do not hesitate to mention the error, what it does/doesn't against you need or anything else, necessary to correct it.
Copy Data to Another Worksheet
Option Explicit
Sub CopyData()
Const sName As String = "Sheet1"
Const rgAddress As String = "G6:J106"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(sName)
Dim rg As Range: Set rg = ws.Range(rgAddress)
WriteCopyData rg
' or just:
'WriteCopyData ThisWorkbook.Worksheets("Sheet1").Range("G6:J106")
End Sub
Sub WriteCopyData(ByVal SourceRange As Range)
Const dName As String = "Sheet2"
Const dRowAddress As String = "C6"
Const dColumnAddress As String = "C5"
Dim rCount As Long: rCount = SourceRange.Rows.Count
Dim cCount As Long: cCount = SourceRange.Columns.Count
Dim dws As Worksheet
Set dws = SourceRange.Worksheet.Parent.Worksheets(dName)
Dim dRow As Long: dRow = dws.Range(dRowAddress).Value
Dim dCol As String: dCol = dws.Range(dColumnAddress).Value
Dim dfrrg As Range: Set dfrrg = dws.Cells(dRow, dCol).Resize(1, cCount)
Dim dlCell As Range
Set dlCell = dfrrg.Resize(dws.Rows.Count - dRow + 1) _
.Find("*", , xlFormulas, , xlByRows, xlPrevious)
If Not dlCell Is Nothing Then
Set dfrrg = dfrrg.Offset(dlCell.Row - dRow + 1)
End If
Dim drg As Range: Set drg = dfrrg.Resize(rCount)
drg.Value = SourceRange.Value
End Sub
I have 2 columns A and B. I created a Sub loop to check if the value of cells in column 2 is <> "NULL" then if its not NULL I have to copy the valueof it and paste it to its counterpart row in Column A.
I tried this code but can't continue because I'm having a hard time pasting the value of the cell in column 2 to its left side column 1 counterpart it only paste in cell A2. How to paste it to every cell in the 1st column if the column 2 counterpart of it is not equal to NULL?
Sub IF_Loop()
Dim cell As Range
For Each cell In Range("TablePrac[Department]")
If cell.Value <> "NULL" Then
cell.Copy Range("A2")
End If
Next cell
End Sub
Copy Values in Excel Table
Before
After
The key difference between the two solutions is that the first 'deals' with the rows and columns of the worksheet, while the second uses the table (DataBodyRange) rows and columns (seems kind of more appropriate).
The 'cValue/CStr business' avoids the type mismatch error occurring if there is an error value.
Adjust the values in the constants section.
The Code
Option Explicit
Sub TableColumns()
Const wsName As String = "Sheet1"
Const dColString As String = "Table1[Column1]"
Const sColString As String = "Table1[Column2]"
Const sCriteria As String = "NULL"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim drg As Range: Set drg = ws.Range(dColString)
drg.ClearContents
Dim dCol As Long: dCol = drg.Column ' Worksheet Column
Dim srg As Range: Set srg = ws.Range(sColString)
Application.ScreenUpdating = False
Dim sCell As Range
Dim cValue As Variant
For Each sCell In srg.Cells
cValue = sCell.Value
If CStr(cValue) <> sCriteria Then
sCell.EntireRow.Columns(dCol).Value = sCell.Value ' Worksheet Row
End If
Next sCell
Application.ScreenUpdating = True
End Sub
Sub TableColumnsRowRange()
Const wsName As String = "Sheet1"
Const tblName As String = "Table1"
Const sColTitle As String = "Column2"
Const sCriteria As String = "NULL"
Const dColTitle As String = "Column1"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim tbl As ListObject: Set tbl = ws.ListObjects(tblName)
Dim sCol As Long: sCol = tbl.ListColumns(sColTitle).Index ' Table Column
Application.ScreenUpdating = False
Dim dCol As Long
With tbl.ListColumns(dColTitle)
.DataBodyRange.ClearContents
dCol = tbl.ListColumns(dColTitle).Index ' Table Column
End With
Dim srrg As Range
Dim cValue As Variant
For Each srrg In tbl.DataBodyRange.Rows ' Table (DataBodyRange) Row
cValue = srrg.Cells(sCol).Value
If CStr(cValue) <> sCriteria Then
srrg.Cells(dCol).Value = srrg.Cells(sCol).Value
End If
Next srrg
Application.ScreenUpdating = True
End Sub
I'm currently working on a Macro that its currently filtering a table based on a value and then it copies the data under a column after the filters have been applied (got that to work). However, I can't figure out how to paste those values in the same table overwriting the data under the visible cells within a different column. Values highlighted in red (picture) are being copied, now I need to paste them over only in the cells highlighted yellow. Thank you!
Public Sub DxcDateUpdate()
Application.ScreenUpdating = False
Dim Mwb As Workbook
Dim ws As Worksheet
Set Mwb = ThisWorkbook
Set ws = Mwb.Worksheets("Commission")
Set ws2 = Mwb.Worksheets("test")
lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
ws.Range("A1").AutoFilter Field:=31, Criteria1:="DXC/TPV.com Enrollment"
ws.Range("AG2:AG" & lr).SpecialCells(xlCellTypeVisible).Copy Destination:=ws.Range '''here is where idk what to do?'''
Application.ScreenUpdating = True
End Sub
You cannot paste a discontinuous range as discontinuous. You should iterate between each range cell and copy it using offset, or building the range to Paste using c.row. Please, try the next adapted code:
Sub DxcDateUpdate()
Dim Mwb As Workbook, ws As Worksheet, rngVis As Range, c As Range, LR As Long
Set Mwb = ThisWorkbook
Set ws = Mwb.Worksheets("Commission")
Set ws2 = Mwb.Worksheets("test")
LR = ws.cells(ws.rows.Count, 1).End(xlUp).row
ws.Range("A1").AutoFilter field:=31, Criteria1:="DXC/TPV.com Enrollment"
Set rngVis = ws.Range("AG2:AG" & LR).SpecialCells(xlCellTypeVisible)
For Each c In rngVis.cells
c.Offset(0, -28).value = c.value
Next
End Sub
In order to make the code faster, of course, you should use some optimization lines (ScreenUpdating = False, EnableEvents = False, Calculation = xlCalculationManual, followed after by True, True, xlCalculationAutomatic).
Copy 'Filtered' Values Using Arrays
The following will loop through the criteria column to find the criteria (string). When found, in the same row, the value from the source column will be copied to the destination column.
The columns' values are written to arrays to speed up the process (the loop).
Option Explicit
Sub DxcDateUpdate()
Const wsName As String = "Commission"
Const fRow As Long = 2
Const cCol As String = "AE" ' Criteria
Const sCol As String = "AG" ' Source
Const dCol As String = "E" ' Destination
Const Criteria As String = "DXC/TPV.com Enrollment"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
Dim lRow As Long: lRow = rg.Rows.Count
Dim cData As Variant: cData = rg.Columns(cCol).Value
Dim sData As Variant: sData = rg.Columns(sCol).Value
With rg.Columns(dCol)
Dim dData As Variant: dData = .Value
Dim r As Long
For r = fRow To lRow
If cData(r, 1) = Criteria Then
dData(r, 1) = sData(r, 1)
End If
Next r
.Value = dData
End With
End Sub