I have a workbook that was previously working with no issues. Recently, however, I have been having a problem with adding/deleting columns on visible sheets after I run certain macros.
The workbook is used for groups of members. When data is imported into the file a base number of members are included. Throughout the use of the file the group can expand or contract. I have macros that will add new members or delete existing ones. These macros simply add data or remove it from specific data sheets. Another macro is used to refresh the keycells range that is used to adjust the columns on the visible sheets.
The issue I am having is that once I either add a new member or delete one, the code to increase or delete columns on the visible sheets does not work. The macro that refreshes the sheet doesn't work, nor does manually adjusting the cell itself.
If I do not import any data and simply add or delete columns from visible sheets (based on changing the keycells value), the code runs perfectly. It seems to only occur when I import data and try using macros that add or delete members. For example, without any data, I can add in 3 members and have new columns added in to each visible sheet. I can then reduce that number manually to 1 or 2 and have the appropriate number of columns deleted for each sheet. This works fine until data is imported and the other mentioned macros are used.
I also am experiencing an issue with the file where once I receive an error, even if I reset the VBA, I cannot continue working in it. I can maneuver throughout the file, however, adding or deleting columns (by any means) does not work. It's as though, even though the VBA was reset in the editor, the code does not exist.
This is the code that is used to refresh the keycell
Sub Refresh_ActivesheetB30()
Dim dwsNames As Variant: dwsNames = Array("DATA Member-19", "DATA Sch A-19", "DATA Sch A-3-19", "DATA Sch J-19", "DATA Sch R-19", "DATA 500U-19", "DATA 500U-P-19", "DATA 500U-PA-19")
frmWait.Show vbModeless
DoEvents
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim wb As Workbook: Set wb = ThisWorkbook
Dim gws As Worksheet: Set gws = wb.Worksheets("GroupInfo")
gws.Range("B30").Formula = "=COUNTIF('TAX INFO'!B34:B1499,"">0"")"
Dim dws As Worksheet
Dim dlRow As Long
Dim d As Long
For d = LBound(dwsNames) To UBound(dwsNames)
On Error Resume Next
Set dws = wb.Worksheets(dwsNames(d))
On Error GoTo 0
If Not dws Is Nothing Then
dlRow = dws.Range("D" & dws.Rows.Count).End(xlUp).Row
dws.Range("A12").Copy dws.Range("A12:A" & dlRow)
Set dws = Nothing
End If
Next d
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
gws.Activate
frmWait.Hide
End Sub
This code adds members
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim N As Long
Dim i As Long
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim Rng4 As Range
Dim Rng5 As Range
Set ws = ActiveSheet
Set Rng1 = ws.Range("6:6").Find(Me.TextBox2.Value)
Set Rng2 = ws.Range("6:6").Find(Me.TextBox6.Value)
Set Rng3 = ws.Range("6:6").Find(Me.TextBox5.Value)
Set Rng4 = ws.Range("6:6").Find(Me.TextBox4.Value)
Set Rng5 = ws.Range("6:6").Find(Me.TextBox7.Value)
N = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
If WorksheetFunction.CountIfs(ws.Range("6:6"), TextBox2, ws.Range("6:6"), TextBox2) = 0 And ComboBox1 <> 0 Then
MsgBox "Sorry, " & TextBox2 & " not found!"
Else
If TextBox3.Value = "" And ComboBox1.Value <> "" Then
MsgBox "There is no data to add", 48
Else
If WorksheetFunction.CountIfs(ws.Range("6:6"), TextBox6, ws.Range("6:6"), TextBox6) = 0 And ComboBox2 <> 0 Then
MsgBox "Sorry, " & TextBox6 & " not found!"
Else
If TextBox8.Value = "" And ComboBox2.Value <> "" Then
MsgBox "There is no data to add", 48
Else
If WorksheetFunction.CountIfs(ws.Range("6:6"), TextBox5, ws.Range("6:6"), TextBox5) = 0 And ComboBox3 <> 0 Then
MsgBox "Sorry, " & TextBox5 & " not found!"
Else
If TextBox9.Value = "" And ComboBox3.Value <> "" Then
MsgBox "There is no data to add", 48
Else
If WorksheetFunction.CountIfs(ws.Range("6:6"), TextBox4, ws.Range("6:6"), TextBox4) = 0 And ComboBox4 <> 0 Then
MsgBox "Sorry, " & TextBox4 & " not found!"
Else
If TextBox10.Value = "" And ComboBox4.Value <> "" Then
MsgBox "There is no data to add", 48
Else
If WorksheetFunction.CountIfs(ws.Range("6:6"), TextBox7, ws.Range("6:6"), TextBox7) = 0 And ComboBox5 <> 0 Then
MsgBox "Sorry, " & TextBox7 & " not found!"
Else
If TextBox11.Value = "" And ComboBox5.Value <> "" Then
MsgBox "There is no data to add", 48
Else
For i = 5 To N
If ActiveSheet.Cells(i, "B").Value = frmAddAdj.ComboBox1.Value Then
ActiveSheet.Cells(i, Rng1.Column).Value = frmAddAdj.TextBox3.Text
End If
If ActiveSheet.Cells(i, "B").Value = frmAddAdj.ComboBox2.Value Then
ActiveSheet.Cells(i, Rng2.Column).Value = frmAddAdj.TextBox8.Text
End If
If ActiveSheet.Cells(i, "B").Value = frmAddAdj.ComboBox3.Value Then
ActiveSheet.Cells(i, Rng3.Column).Value = frmAddAdj.TextBox9.Text
End If
If ActiveSheet.Cells(i, "B").Value = frmAddAdj.ComboBox4.Value Then
ActiveSheet.Cells(i, Rng4.Column).Value = frmAddAdj.TextBox10.Text
End If
If ActiveSheet.Cells(i, "B").Value = frmAddAdj.ComboBox5.Value Then
ActiveSheet.Cells(i, Rng5.Column).Value = frmAddAdj.TextBox11.Text
End If
Next i
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
Private Sub CommandButton2_Click()
Unload frmAddAdj
End Sub
Private Sub CommandButton3_Click()
Dim ctl As MSForms.Control
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox", "ComboBox"
ctl.Text = ""
Case "CheckBox", "OptionButton", "ToggleButton"
ctl.Value = False
End Select
Next ctl
End Sub
Private Sub UserForm_Initialize()
Dim iRow As Integer, iMax As Integer
iRow = Cells.Find(What:="New Jersey Audit Adjustment", _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
iMax = Cells.Find(What:="New Jersey Audit Adjustment", _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False, SearchFormat:=False).Row
If ActiveSheet.Range("B" & iRow & ":B" & iMax).Cells.Count = 1 Then
Me.ComboBox1.AddItem ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox2.AddItem ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox3.AddItem ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox4.AddItem ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox5.AddItem ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Else
Me.ComboBox1.List = ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox2.List = ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox3.List = ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox4.List = ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox5.List = ActiveSheet.Range("B" & iRow & ":B" & iMax).Value
End If
End Sub
This is the code to delete members
Private Sub CommandButton1_Click()
'declare the variables
Dim Findvalue As Range, DeleteRange As Range
Dim Response As VbMsgBoxResult
Dim cNum As Integer
Dim Search As String, FirstAddress As String
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("DATA Member-19")
'error statement
On Error Resume Next
Search = TextBox6.Value
'check for control from listbox dblclick values
If TextBox6.Value = "" Or Search = "" Then
MsgBox "There is not data to delete", 48
Exit Sub
Else
'find the employees number row
Set Findvalue = ws.Range("D:D").Find(What:=Search, LookIn:=xlValues, LookAt:=xlWhole)
If Not Findvalue Is Nothing Then
'mark first address
FirstAddress = Findvalue.Address
'give the user a chance to change their mind!
Response = MsgBox(Search & Chr(10) & _
"Are you sure that you want to delete this Member?", 292, "Are you sure?")
If Response = vbYes Then
'find all matching records
Do
If DeleteRange Is Nothing Then
Set DeleteRange = Findvalue
Else
Set DeleteRange = Union(DeleteRange, Findvalue)
End If
Set Findvalue = ws.Range("D:D").FindNext(Findvalue)
Loop While FirstAddress <> Findvalue.Address
'delete record(s)
DeleteRange.EntireRow.Delete
'clear the user form controls
cNum = 12
For x = 1 To cNum
Me.Controls("Reg" & x).Value = ""
Next
'Employee deleted from the database
MsgBox Search & Chr(10) & "The Member has been deleted successfully.", 64, "Record Deleted"
'add the values to the listbox
lstLookup.RowSource = ""
End If
Else
MsgBox Search & Chr(10) & "Record Not Found", 48, "Not Found"
End If
End If
End Sub
Private Sub CommandButton2_Click()
Unload frmDeleteMembers19
End Sub
This is the code that goes into the main sheet module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range, colNum As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
SOMESHEETS = "*C-Proposal-19*MemberInfo-19*Schedule J-19*NOL-19*NOL-P-19*NOL-PA-19*Schedule R-19*Schedule A-3-19*Schedule A-19*Schedule H-19*"
Set KeyCells = Range("B30")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
If IsNumeric(KeyCells.Value) Then
colNum = KeyCells.Value
If colNum > 0 Then
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
If CBool(InStr(LCase(SOMESHEETS), LCase("*" & ws.Name & "*"))) Then
InsertColumnsOnSheet argSheet:=ws, argColNum:=colNum
End If
End If
Next ws
End If
End If
End If
SOMESHEETS = "*MemberInfo-20*C-Proposal-20*Schedule J-20*NOL-20*Schedule R-20*NOL-P-20*SchA-3-20*Schedule H-20*NOL-PA-20*Schedule A-20*Schedule A-5-20*"
Set KeyCells = Range("B36")
If Not Application.Intersect(KeyCells, Target) Is Nothing Then
If IsNumeric(KeyCells.Value) Then
colNum = KeyCells.Value
If colNum > 0 Then
For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
If CBool(InStr(LCase(SOMESHEETS), LCase("*" & ws.Name & "*"))) Then
InsertColumnsOnSheet argSheet:=ws, argColNum:=colNum
End If
End If
Next ws
End If
End If
End If
Application.ScreenUpdating = True
End Sub
And this is the general code that each sheet pulls from. I only included on sheet to save some space, but each sheet has similar code.
Public Sub InsertColumnsOnSheet(ByVal argSheet As Worksheet, ByVal argColNum As Long)
Dim Rng As Range, c As Range
Dim TotalCol As Long, LeftFixedCol As Long
Dim i As Long
Dim ws As Worksheet
Dim j As Integer, k As Integer
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Set ws = Worksheets("C-Proposal-19")
With argSheet
Set Rng = .Range(.Cells(3, 6), .Cells(3, .Columns.Count))
Set c = Rng.Find("GROSS")
If Not c Is Nothing Then
TotalCol = c.Column
LeftFixedCol = 5
j = .Range("B4").End(xlToRight).Column
k = j - LeftFixedCol
If ws.Visible = xlSheetVisible Then
If TotalCol < LeftFixedCol + argColNum + 1 Then
.Columns(j).Copy
.Columns(j + 1).Resize(, argColNum - k).Insert CopyOrigin:=xlFormatFromLeftOrAbove
Application.CutCopyMode = False
End If
End If
If TotalCol > LeftFixedCol + argColNum + 1 Then
For i = TotalCol - 1 To LeftFixedCol + argColNum + 1 Step -1
.Columns(i).Delete
Next i
End If
End If
End With
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Moreover, if I add a member and refresh the keycells range, a new column will be added. If I then try to reduce the amount of columns manually, it will reduce the amount of columns. The issue pops up when I try use the delete macro. After I delete out the member through that macro, I cannot add or delete columns, either manually or through the refresh macro.
There also seems to be an issue where if I have an error pop up with VBA, the workbook does not use the code that I have in it. For example, if I change the general code to add/delete columns and I get an error, even if I reset the VBA in the editor, if I try and change the keycells nothing happens. I don't get the same error again, even though I don't change the code at all, and nothing happens to any of the sheets.
There is a bug in the first part of your code:
For d = LBound(dwsNames) To UBound(dwsNames)
On Error Resume Next
Set dws = wb.Worksheets(dwsNames(d))
On Error GoTo 0
If Not dws Is Nothing Then
dlRow = dws.Range("D" & dws.Rows.Count).End(xlUp).Row
dws.Range("A12").Copy dws.Range("A12:A" & dlRow)
Set dws = Nothing
End If
Next d
This shows the problem:
Dim ws As Worksheet, e
For Each e In Array("Sheet1", "Sheet2")
On Error Resume Next
Set ws = ThisWorkbook.Sheets(e)
On Error GoTo 0
If Not ws Is Nothing Then Debug.Print e, ws.Name
Next e
Run in a workbook which contains only Sheet1, it gives this output:
Sheet1 Sheet1
Sheet2 Sheet1 'oops!
So you need to add Set dws = Nothing before running Set dws = wb.Worksheets(dwsNames(d))
I have been wracking my brain over this one for quite a while now, I am trying to automate a work-task tracker as much as possible and I have hit a wall. It is a simple cut and paste macro; but I am trying to add a variable that might be getting thrown off by a formula in my spreadsheet. Basically I'd like the macro to look at column "F" and if it is "Closed" and if Column "G" has "Dec" (or "Jan" etc) then it will cut and paste to the corresponding sheet.
Private Sub CommandButton1_Click()
Dim Check As Range, r As Long, lastrow2 As Long, lastrow As Long, theDate As Date
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
Application.EnableEvents = False
lastrow = Worksheets("Master list").UsedRange.Rows.Count
lastrow2 = Worksheets("Dec").UsedRange.Rows.Count
If lastrow2 = 1 Then lastrow2 = 0
For r = lastrow To 2 Step -1
If Range("F" & r).Value = "Closed" And Range("G" & r).Value = "Dec" Then
Rows(r).Cut Destination:=Worksheets("Dec").Range("A" & lastrow2 + 1) ' And here
lastrow2 = lastrow2 + 1
Else:
End If
Next r
Dim i As Long
Dim DelRange As Range
On Error GoTo Whoa
For i = 1 To 250
If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "Z" & i)) = 0 Then
If DelRange Is Nothing Then
Set DelRange = Rows(i)
Else
Set DelRange = Union(DelRange, Rows(i))
End If
End If
Next i
If Not DelRange Is Nothing Then DelRange.Delete Shift:=xlUp
LetsContinue:
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox "Perfectly balanced, as all things should be."
End Sub
Ries
I'm trying to get my spreadsheet to automatically generate new names and sheets based on a data dump. I currently have the sheet working so that it will generate the name and sheet for each row of data, but I cannot get it to populate the sheet using that row.
There is a specific section of code that I cannot get to work:
For Each Nm In shNAMES
If Not Evaluate("ISREF('" & CStr(Nm.Text) & "'!A1)") Then
wsTEMP.Copy After:=.Sheets(.Sheets.Count)
ActiveSheet.Name = CStr(Nm.Text)
ActiveSheet.Range("A1:I1").Value = wsMASTER.Range("A" & Nm & ":I" & Nm) ' <<< This line here
End If
Next Nm
I know that the issue is using Nm to reference the cell (it's returning "OP01" which is the cell contents), but I'm trying to not add another workaround. I've tried using other functions to do similar after the Nm loop has finished, but can't seem to get those working either. Surely the answer has to be simple and I'm just missing something?
Option Explicit
Sub SheetsFromTemplate()
Application.ScreenUpdating = False
Rows("1:8").EntireRow.Delete
Call CreateLONums
Dim wsMASTER As Worksheet, wsTEMP As Worksheet, wsINDEX As Worksheet, wasVISIBLE As Boolean
Dim shNAMES As Range, Nm As Range
Dim firstrow As Long, lastrow As Long
With ThisWorkbook
Set wsTEMP = .Sheets("Template")
wasVISIBLE = (wsTEMP.Visible = xlSheetVisible)
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetVisible
Set wsMASTER = .Sheets("Creation Page")
Set wsINDEX = .Sheets("Local Options")
With Sheets("Creation Page").Columns("A")
If WorksheetFunction.CountA(.Cells) = 0 Then
MsgBox "Sorry: no data"
Else
With .SpecialCells(xlCellTypeConstants)
firstrow = .Areas(1).Row
lastrow = .Areas(.Areas.Count).Cells(.Areas(.Areas.Count).Rows.Count).Row
End With
End If
End With
Set shNAMES = wsMASTER.Range("A" & firstrow, "A" & lastrow)
For Each Nm In shNAMES
If Not Evaluate("ISREF('" & CStr(Nm.Text) & "'!A1)") Then
wsTEMP.Copy After:=.Sheets(.Sheets.Count)
ActiveSheet.Name = CStr(Nm.Text)
ActiveSheet.Range("A1:I1").Value = wsMASTER.Range("A" & Nm & ":I" & Nm)
End If
Next Nm
wsINDEX.Activate
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetHidden
End With
Worksheets("Creation Page").Delete
Worksheets("Template").Delete
Call CreateLinksToAllSheets
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub CreateLONums()
Dim firstrow As Long, lastrow As Long, rowcount As Integer
Columns("A:A").Insert Shift:=xlToRight
With Sheets("Creation Page").Columns("B")
If WorksheetFunction.CountA(.Cells) = 0 Then
MsgBox "Sorry: no data"
Else
With .SpecialCells(xlCellTypeConstants)
firstrow = .Areas(1).Row
lastrow = .Areas(.Areas.Count).Cells(.Areas(.Areas.Count).Rows.Count).Row
End With
End If
For rowcount = firstrow To firstrow + 9
Range("A" & rowcount).Value = "OP0" & rowcount - firstrow + 1
Next rowcount
For rowcount = firstrow + 9 To lastrow
Range("A" & rowcount).Value = "OP" & rowcount - firstrow + 1
Next rowcount
End With
End Sub
Appreciate any insight available.
Managed to work it out, took way longer than it should have -.-'
I borrowed a bit of Function code to reference the number from column A, then used that to reference the cells that I wanted.
For Each Nm In shNAMES
rownum = GetDigits(Nm) 'This bit here is calling the function
If Not Evaluate("ISREF('" & CStr(Nm.Text) & "'!A1)") Then
wsTEMP.Copy after:=.Sheets(.Sheets.Count)
ActiveSheet.Name = CStr(Nm.Text)
ActiveSheet.Range("A1:J1").Value = wsMASTER.Range("A" & rownum & ":J" & rownum).Value `This is utilising rownum to reference the cells
End If
Next Nm
Function code can be found here: How to find numbers from a string?
Entire code section in case it is useful to someone in future:
Option Explicit
Sub SheetsFromTemplate()
Application.ScreenUpdating = False
Rows("1:8").EntireRow.Delete
Call CreateLONums
Dim wsMASTER As Worksheet, wsTEMP As Worksheet, wsINDEX As Worksheet, wasVISIBLE As Boolean
Dim shNAMES As Range, Nm As Range
Dim firstrow As Long, lastrow As Long, rownum As Integer
With ThisWorkbook
Set wsTEMP = .Sheets("Template")
wasVISIBLE = (wsTEMP.Visible = xlSheetVisible)
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetVisible
Set wsMASTER = .Sheets("Creation Page")
Set wsINDEX = .Sheets("Local Options")
With Sheets("Creation Page").Columns("A")
If WorksheetFunction.CountA(.Cells) = 0 Then
MsgBox "No Data Available"
Else
With .SpecialCells(xlCellTypeConstants)
firstrow = .Areas(1).Row
lastrow = .Areas(.Areas.Count).Cells(.Areas(.Areas.Count).Rows.Count).Row
End With
End If
End With
Set shNAMES = wsMASTER.Range("A" & firstrow, "A" & lastrow)
For Each Nm In shNAMES
rownum = GetDigits(Nm)
If Not Evaluate("ISREF('" & CStr(Nm.Text) & "'!A1)") Then
wsTEMP.Copy after:=.Sheets(.Sheets.Count)
ActiveSheet.Name = CStr(Nm.Text)
ActiveSheet.Range("A1:J1").Value = wsMASTER.Range("A" & rownum & ":J" & rownum).Value
End If
Next Nm
wsINDEX.Activate
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetHidden
End With
Worksheets("Template").Move after:=Worksheets(Worksheets.Count)
Worksheets("Creation Page").Move after:=Worksheets(Worksheets.Count)
Call CreateLinksToAllSheets
Call UpdateIndexTechSpec
Call UpdateIndexOptDescription
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub CreateLONums()
Dim firstrow As Long, lastrow As Long, rowcount As Integer
Columns("A:A").Insert Shift:=xlToRight
With Sheets("Creation Page").Columns("B")
If WorksheetFunction.CountA(.Cells) = 0 Then
MsgBox "Sorry: no data"
Else
With .SpecialCells(xlCellTypeConstants)
firstrow = .Areas(1).Row
lastrow = .Areas(.Areas.Count).Cells(.Areas(.Areas.Count).Rows.Count).Row
End With
End If
For rowcount = firstrow To firstrow + 9
Range("A" & rowcount).Value = "OP0" & rowcount - firstrow + 1
Next rowcount
For rowcount = firstrow + 9 To lastrow
Range("A" & rowcount).Value = "OP" & rowcount - firstrow + 1
Next rowcount
End With
End Sub
Sub CreateLinksToAllSheets()
Dim sh As Worksheet
Dim cell As Range
Sheets("Local Options").Activate
ActiveSheet.Cells(10, 1).Select
For Each sh In ActiveWorkbook.Worksheets
If ActiveSheet.Name <> sh.Name Then
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
ActiveCell.Offset(1, 0).Select
End If
Next sh
Call UpdateIndexTechSpec
Call UpdateIndexOptDescription
End Sub
Sub UpdateIndexTechSpec()
Dim sh As Worksheet
Dim cell As Range
Sheets("Local Options").Activate
ActiveSheet.Cells(10, 2).Select
For Each sh In ActiveWorkbook.Worksheets
If ActiveSheet.Name <> sh.Name Then
ActiveCell.Value = sh.Range("B2").Value
ActiveCell.Offset(1, 0).Select
End If
Next sh
End Sub
Sub UpdateIndexOptDescription()
Dim sh As Worksheet
Dim cell As Range
Sheets("Local Options").Activate
ActiveSheet.Cells(10, 3).Select
For Each sh In ActiveWorkbook.Worksheets
If ActiveSheet.Name <> sh.Name Then
ActiveCell.Value = sh.Range("D2").Value
ActiveCell.Offset(1, 0).Select
End If
Next sh
End Sub
Function GetDigits(ByVal s As String) As String
Dim char As String
Dim i As Integer
GetDigits = ""
For i = 1 To Len(s)
char = Mid(s, i, 1)
If char >= "0" And char <= "9" Then
GetDigits = GetDigits + char
End If
Next i
End Function
I need to merge rows with same values on excel - I tried using pivot tables, and consolidate and I could not get the desired output. I need rows with same columns merged
Try,
Sub test()
Dim rngDB As Range
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
Set rngDB = Range("b2", Range("b" & Rows.Count).End(xlUp))
MergeRange rngDB
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Sub MergeRange(rngDB As Range)
Dim rng As Range
Dim rngO As Range, myCell As Range
Dim n As Integer
For Each rng In rngDB
If rng <> "" Then
n = WorksheetFunction.CountIf(rngDB, rng)
Set rngO = rng.Offset(, 1).Resize(n)
MergeRange rngO
For Each myCell In rngO
If myCell <> "" Then
myCell.Resize(WorksheetFunction.CountIf(rngO, myCell)).Merge
End If
Next myCell
rng.Resize(n).Merge
End If
Next rng
End Sub
Which column are you testing to do the consolidation? Is it B or C? Anyway, try this, and adjust the code to suit your specific needs.
Sub Macro()
Dim lngRow As Long
For lngRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
If StrComp(Range("B" & lngRow), Range("B" & lngRow - 1), vbTextCompare) = 0 Then
If Range("C" & lngRow) <> "" Then
Range("C" & lngRow - 1) = Range("C" & lngRow - 1) & "; " & Range("C" & lngRow)
End If
Rows(lngRow).Delete
End If
Next
End Sub
Before:
After:
I have a code that looks at Column K, checks if there is a 0 and if there is, it deletes the corresponding rows from C to K.
Sub del()
Application.ScreenUpdating = False 'Prevent screen flickering
Application.Calculation = xlCalculationManual 'Preventing calculation
Dim sh As Worksheet, lr As Long, i As Long, lngStartRow As Long
Set sh = Sheets("Formations_Tracker")
lr = sh.Cells(Rows.Count, "C").End(xlUp).Row
lngStartRow = 2 'Starting data row number.
For i = lr To lngStartRow Step -1
If sh.Cells(i, "K") = 0 Then
sh.Cells(i, "K").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "J").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "I").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "H").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "G").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "F").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "E").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "D").Resize(1, 2).Delete Shift:=xlUp
sh.Cells(i, "C").Resize(1, 2).Delete Shift:=xlUp
End If
Next i
Set sh = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
The issue is this works if the last row is the one that contains a 0. However if it's not the last row, it seems to be deleting more rows, even the ones that don't have 0s in them.
You can reduce that loop to a simple filter and delete. Note this is deleting the entire row so this may need some modification on your end to suit your needs
Sub del()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Formations_Tracker")
Dim LR As Long
Dim DeleteMe As Range
LR = ws.Range("K" & ws.Rows.Count).End(xlUp).Row
Application.DisplayAlerts = False
ws.Range("C1:K" & LR).AutoFilter Field:=9, Criteria1:=0
Set DeleteMe = ws.Range("C2:K" & LR).SpecialCells(xlCellTypeVisible)
ws.AutoFilterMode = False
If Not DeleteMe Is Nothing Then DeleteMe.Delete (xlShiftUp)
Application.DisplayAlerts = True
End Sub
Here is another approach:
Option Explicit
Sub del()
Application.ScreenUpdating = False 'Prevent screen flickering
Application.Calculation = xlCalculationManual 'Preventing calculation
'you should also reference the workbook
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Formations_Tracker")
'ThisWorkbook refers to the workbook which contains the code
Dim lngStartRow As Long
lngStartRow = 2 'Starting data row number.
Dim lr As Long
lr = sh.Cells(Rows.Count, "C").End(xlUp).Row
'When looping through cells is always better to use the For Each
Dim C As Range
'It would be wise to delete everything at once using a range to delete
Dim DelRange As Range
For Each C In sh.Range("K" & lngStartRow & ":K" & lr)
If C = 0 Then
If DelRange Is Nothing Then
Set DelRange = C
Else
Set DelRange = Union(DelRange, C)
End If
End If
Next C
'Delete all your rows at once if there is a match
If Not DelRange Is Nothing Then DelRange.EntireRow.Delete
Set sh = Nothing
Set DelRange = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub