Currently realising a project for my company to highlight anomalies in orders, I wrote this VBA code in order to compare status of orders and a day date difference between the current date and the order date . The dates are as follows on the csv : "2019-05-06 3:11pm"
When I affecte the macro to a button and click on it I get this error : "Compilation error, Sub or function not defined" and the debugger cursor points on "Sub Problem()"...
I hope you can help me!! thanks by advance :)
here is my code :
Sub Problem()
Dim orderDate As Date
Dim difDate
Dim statusToUse As Range
Set statusToUse = Range("C2:C100")
Dim statusCell As Range
Dim a As String
a = "accepted"
Dim s As String
s = "shipped"
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim dateToUse As Range
Set dateToUse = Range("D2:D100")
Dim dateCell As Range
Dim currentDate As String
currentDate = Date
y = Split(Split(Range("A1").Value, ":")(1), "-")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each statusCell In statusToUse.Areas
For Each dateCell In dateToUse.Areas
orderDate = DateValue(Join(Array(y(2), y(1), y(0)), "-"))
difDate = DateDiff("d", currentDate, orderDate)
If status = a And difDate <= 7 Then
status.Interior.ColorIndex = 46
i = i + 1
ElseIf status = a And difDate <= 2 Then
status.Interior.ColorIndex = 27
j = j + 1
ElseIf status = a And difDate > 7 Then
status.Interior.ColorIndex = 3
k = k + 1
ElseIf status = s Then
status.Interior.ColorIndex = 10
l = l + 1
Else
m = m + 1
End If
i = i - j
Next dateCell
Next statusCell
MsgBox "There are" + i + "risky orders"
End Sub
This error usually occurs when the sub is in the worksheet and not in a new module.
If it's in a module already, please check if the name of the module is different of "module1".
Related
I created a lookup function that finds the result from a separate tab within the same worksheet with 4 different fields to match.
When running, this takes entirely too long to complete (to the point where I have to kill the macro run). I need to build the same lookup function for 8 different fields, based on the exact same match criteria. Any advice on how to speed up this query or build it in a more dynamic way, so I can lookup all 8 columns at once rather than building functions and subs for each lookup field?
Function fcst_bal_find(ByVal Anode As String, ByVal LoB As String, ByVal Month As String, ByVal Year As String) As Variant
Dim Fcst_Essbase As Worksheet
Dim fcst_rowcnt
Dim act_rowcnt
fcst_rowcnt = Sheets("Date Dims").Range("B7")
act_rowcnt = Sheets("Date Dims").Range("B8")
Set Fcst_Essbase = Sheets("Fcst Essbase Pull")
For i = 2 To fcst_rowcnt + 4
If WorksheetFunction.Trim(Fcst_Essbase.Cells(i, 1).Value) = Anode Then
If WorksheetFunction.Trim(Fcst_Essbase.Cells(i, 2).Value) = LoB Then
If WorksheetFunction.Trim(Fcst_Essbase.Cells(i, 3).Value) = Month Then
If "Y" & Right(WorksheetFunction.Trim(Fcst_Essbase.Cells(i, 4).Value), 2) = Year Then
fcst_bal_find = Fcst_Essbase.Cells(i, 5).Value
Exit Function
End If
End If
End If
End If
Next i
fcst_bal_find = "N/A"
End Function
Sub balfcst_find()
Dim fcst_tab As Worksheet
Dim match As Variant
Dim Anode As String
Dim LoB As String
Dim Month As String
Dim Year As String
Dim fcst_rowcnt
Dim act_rowcnt
fcst_rowcnt = Sheets("Date Dims").Range("B7")
act_rowcnt = Sheets("Date Dims").Range("B8")
Set fcst_tab = Sheets("Cartesian Product - Fcst")
For i = 2 To fcst_rowcnt
Anode = fcst_tab.Range("A" & i).Value
LoB = fcst_tab.Range("B" & i).Value
Month = fcst_tab.Range("C" & i).Value
Year = fcst_tab.Range("D" & i).Value
match = fcst_bal_find(Anode, LoB, Month, Year)
fcst_tab.Cells(i, 5) = match ' test the output
Next i
End Sub
Here is an example of using variant array to match something from a current project of mine. You can modify to suit your needs.
Private Function verifyMod(modValue As Double, state As String) As Boolean
If Len(modValue) Then
Dim modTable As ListObject
Set modTable = lookupsAUState.ListObjects("stateWritingCoModMinMax")
Dim v As Variant
v = modTable.DataBodyRange.value
Dim company As String
company = StrConv(xmlCo.Range("insuranceCompanyName"), vbProperCase)
Dim x As Long
For x = LBound(v) To UBound(v)
If v(x, modTable.ListColumns("Company").index) = company Then
If v(x, modTable.ListColumns("State").index) = state Then
If modValue >= v(x, modTable.ListColumns("Min").index) And modValue <= v(x, modTable.ListColumns("Max").index) Then
verifyMod = True
Else
MsgBox state & " allows for modifications between " & v(x, modTable.ListColumns("Min").index) & " and " & v(x, modTable.ListColumns("Max").index) & ". Please enter a modification within that range."
End If
Exit For
End If
End If
Next
End If
End Function
I have a excel document with a lot of data. I have four columns that I use.
fiscal year
Gain/loss on ineffective hedges
SIC code.
Sorted unique SIC code so no one appears more than once.
I have created 4 arrays and stored the values in them.
What I want to do, but have some problems with right now, is to go through each year (from 2001-2018) and calculate the average gain/loss(2) for each SIC code, for each year. I have written the following code:
Sub Assignment4()
Dim Wb As Workbook
Dim Ws As Worksheet
Set Wb = ThisWorkbook
Set Ws = ThisWorkbook.Worksheets("Sheet1")
Dim iLastRow As Integer, iLastRow2 As Integer
Dim readStart As Integer
Dim count As Integer
Dim ArrayFiscalYear() As String
Dim ArrayGL() As String
Dim ArraySIC() As Variant
Dim ArraySICSorted() As Integer
ReDim ArrayFiscalYear(10000)
ReDim ArrayGL(10000)
ReDim ArraySIC(10000)
ReDim ArraySICSorted(10000)
Dim year As Integer
Dim arr As New Collection, a
Dim j As Long
Dim x As Long
Dim avg As Double
Dim sum As Double
Dim counter As Integer
iLastRow = Ws.Cells(Rows.count, "J").End(xlUp).Row
count = 2
readStart = 2
Do While (count <= iLastRow)
ArraySIC(count) = Ws.Cells(count, 10)
count = count + 1
Loop
Ws.Range("J2:J" & iLastRow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Ws.Range("L1"), _
Unique:=True
Ws.Cells(1, 12) = "Sorted SIC"
iLastRow2 = Ws.Cells(Rows.count, "L").End(xlUp).Row
Debug.Print (iLastRow2)
count = 0
counter = 2
Do While (count <= iLastRow2)
ArraySICSorted(count) = Ws.Cells(counter, 12)
Debug.Print (ArraySICSorted(count))
count = count + 1
counter = counter + 1
Loop
Ws.Columns(12).EntireColumn.Delete
i = 2
year = 2001
count = 0
x = 1
Do While year <= 2018
For x = LBound(ArraySICSorted) To UBound(ArraySICSorted)
For i = 1 To iLastRow
If Ws.Cells(i, 3) = year Then
ArrayFiscalYear(year) = Ws.Cells(i, 3)
ArrayGL(year) = Ws.Cells(i, 8)
ArraySIC(year) = Ws.Cells(i, 10)
count = count + 1
sum = sum + ArrayGL(year)
End If
Next i
avg = sum / count
Debug.Print (count & " --- " & ArraySICSorted(x) & " --- " & year & " --- " & x)
sum = 0
count = 0
If x >= 62 Then
year = year + 1
Exit For
End If
Next x
Loop
End Sub
I have attached a screen shot of my excel document
I hope someone can tell me what I do wrong so I can learn from this.
The below Code Counts the number of unique names is a specific column after inputting the data into an array. It works perfectly when running in the the immediate window. But when using as a UDF it throws #Value Error. I am taking all the data into an array and checking the array and getting a number out of it and returning it. I am not modifying any excel sheets or changing the worksheet's environment. Please help!!1
Public Function Operator_Count(Aircraft As String) As Integer
Dim Aircraft_Name As String
Dim Data_Array() As Variant
Dim Row_Count As Integer
Dim Col_Count As Integer
Dim Col_Alph As String
Dim Row_Counter As Integer
Dim Master_Series_Column As Integer
Dim Status_Column As Integer
Dim Operator_Column As Integer
Dim InnerLoop_Counter As Integer
Dim Operator_Array() As Variant
Dim Operator_Array_Transpose() As Variant
Dim Array_Counter As Integer
Aircraft_Name = Aircraft
Operator_Count = 0
'ThisWorkbook.Sheets("Aircraft Data").Activate
Row_Count = ThisWorkbook.Sheets("Aircraft Data").Range("A2", Range("A2").End(xlDown)).Rows.Count
Col_Count = ThisWorkbook.Sheets("Aircraft Data").Cells(1, Columns.Count).End(xlToLeft).Column
Col_Alph = ColumnLetter(Col_Count)
Data_Array = ThisWorkbook.Sheets("Aircraft Data").Range("A1:" & Col_Alph & Row_Count + 1).Value2
For Row_Counter = 1 To Col_Count
If Data_Array(1, Row_Counter) = "Master Series" Then
Master_Series_Column = Row_Counter
End If
Next
For Row_Counter = 1 To Col_Count
If Data_Array(1, Row_Counter) = "Status" Then
Status_Column = Row_Counter
End If
Next
For Row_Counter = 1 To Col_Count
If Data_Array(1, Row_Counter) = "Operator" Then
Operator_Column = Row_Counter
End If
Next
'Resizing the data array
ReDim Operator_Array(0, 0)
'Adding column to the data array
InnerLoop_Counter = 0
For Row_Counter = 1 To UBound(Data_Array)
If Data_Array(Row_Counter, Master_Series_Column) = Aircraft_Name And (Data_Array(Row_Counter, Status_Column) = "In Service" Or Data_Array(Row_Counter, Status_Column) = "On order") Then
Flag = 0
For Array_Counter = 0 To UBound(Operator_Array, 2)
If Operator_Array(0, Array_Counter) = Data_Array(Row_Counter, Operator_Column) Then
Flag = 1
Array_Counter = UBound(Operator_Array, 2)
End If
Next
If Flag <> 1 Then
ReDim Preserve Operator_Array(0, InnerLoop_Counter)
Operator_Array(0, InnerLoop_Counter) = Data_Array(Row_Counter, Operator_Column)
InnerLoop_Counter = InnerLoop_Counter + 1
End If
End If
Next
Operator_Count = UBound(Operator_Array, 2)
End Function
Function ColumnLetter(ColumnNumber As Integer) As String
Dim n As Integer
Dim c As Byte
Dim s As String
n = ColumnNumber
Do
c = ((n - 1) Mod 26)
s = Chr(c + 65) & s
n = (n - c) \ 26
Loop While n > 0
ColumnLetter = s
End Function
I'm trying to export DataGridView items to Excel file and everything perfect, but I want the gridlines appear when the client want to print the sheet, I can do this from inside excel as shown here.
but how can I do this from the vb.net code ??
and I have a problem that : I cant make the text Alignment center I tried this code:
wSheet.Range("a2", "z1000").HorizontalAlignment = excel.XlVAlign.xlVAlignCenter
but it show this error: Public member 'XlVAlign' on type 'ApplicationClass' not found.
Is there a different way to make all columns Alignment center.
this Is my code :
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
Exit Sub
End If
'Creating dataset to export
Dim dset As New DataSet
'add table to dataset
dset.Tables.Add()
'add column to that table
For i As Integer = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(i).Visible = True Then
dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
End If
Next
Dim celltext As String
Dim count As Integer = -1
'add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To DataGridView1.Columns.Count - 1
If DataGridView1.Columns(j).Visible = True Then
count = count + 1
dr1(count) = DataGridView1.Rows(i).Cells(j).Value
End If
Next
count = -1
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Excel.Application
Dim wBook As Excel.Workbook
Dim wSheet As Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
' for the header
wSheet.Rows(1).Font.Name = "Droid Arabic Kufi"
wSheet.Rows(1).Font.size = 11
wSheet.Rows(1).Font.Bold = True
wSheet.Rows(1).HorizontalAlignment = HorizontalAlignment.Right
Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#20b2aa")
wSheet.Rows(1).Font.color = mycol
' for all the sheet without header
wSheet.Range("a2", "z1000").Font.Name = "Droid Arabic Kufi"
wSheet.Range("a2", "z1000").Font.Size = 10
wSheet.Range("a2", "z1000").HorizontalAlignment = excel.XlVAlign.xlVAlignCenter
wSheet.Range("A1:X1").EntireColumn.AutoFit()
wSheet.Range("A1:X1").EntireRow.AutoFit()
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Excel Workbook|*.xls|Excel Workbook 2011|*.xlsx"
saveFileDialog1.Title = "Save Excel File"
saveFileDialog1.FileName = "Export " & Now.ToShortDateString & ".xlsx"
saveFileDialog1.ShowDialog()
saveFileDialog1.InitialDirectory = "C:/"
If saveFileDialog1.FileName <> "" Then
Dim fs As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)
fs.Close()
End If
Dim strFileName As String = saveFileDialog1.FileName
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
Exit Sub
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName)
excel.Workbooks.Open(strFileName)
excel.Visible = True
Exit Sub
errorhandler:
MsgBox(Err.Description)
End Sub
And Is there a way to make rows In different color like row 1 background color blue Row 2 background color white, row 2 background color blue Row 4 background color ...etc
Note :
what is the default :
what I want :
First change the following code:
wSheet.Range("a2", "z1000").HorizontalAlignment = excel.XlVAlign.xlVAlignCenter
TO
wSheet.Range("a2", "z1000").HorizontalAlignment = excel.XlHAlign.xlHAlignCenter
Cell font color , size
Dim formatRange As Excel.Range
formatRange = xlWorkSheet.Range("b1", "b1")
formatRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
formatRange.Font.Size = 10
xlWorkSheet.Cells(1, 2) = "Red"
Add border to a specific cell
Dim formatRange As Excel.Range = xlWorkSheet.UsedRange
Dim cell As Excel.Range = formatRange.Cells(3, 3)
Dim border As Excel.Borders = cell.Borders
border.LineStyle = Excel.XlLineStyle.xlContinuous
border.Weight = 2.0
Border around multiple cells in excel
Dim formatRange As Excel.Range = wSheet.UsedRange
Dim cell As Excel.Range = wSheet.Range("a1", "e" & DataGridView1.RowCount & "")
Dim border As Excel.Borders = cell.Borders
border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
border.Weight = 2.0
Read more in this Article
Thank to H.Fadlallah for helping me
This the answer:
make Gridlines to the excel sheet :
Dim formatRange As Excel.Range = wSheet.UsedRange
Dim cell As Excel.Range = wSheet.Range("a1", "j" & DataGridView1.RowCount + 1 & "")
Dim border As Excel.Borders = cell.Borders
border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
border.Weight = 1.0
And this code to make Alignment center :
wSheet.Range("a2", "z1000").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
And this is the All Code To export DataGridView To Excel:
Button code :
Private Sub To_Excel_picbox_but_Click(sender As Object, e As EventArgs) Handles To_Excel_picbox_but.Click
Try
Dim day As Integer = Date.Today.Day
Dim month As Integer = Date.Today.Month
Dim year As Integer = Date.Today.Year
SaveFileDialog1.Filter = "Excel File|*.xlsx"
SaveFileDialog1.Title = "Save an Excel File"
SaveFileDialog1.FileName = " الحوالات المرسلة" & day & "-" & month & "-" & year & ".xlsx"
SaveFileDialog1.InitialDirectory = "C:/"
Application.EnableVisualStyles()
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
If SaveFileDialog1.FileName <> "" Then
BackgroundWorker2.RunWorkerAsync()
Dim fs As System.IO.FileStream = CType(SaveFileDialog1.OpenFile(), System.IO.FileStream)
fs.Close()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
BackGroundWorker do work code:
Private Sub BackgroundWorker2_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker2.DoWork
ExporttoExcel(DataGridView1)
End Sub
Export Data To Excel Code:
Sub ExporttoExcel(ByVal DataGridView1 As DataGridView)
'verfying the datagridview having data or not
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
Exit Sub
End If
'Creating dataset to export
Dim dset As New DataSet
'add table to dataset
dset.Tables.Add()
'add column to that table
For i As Integer = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(i).Visible = True Then
dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
End If
Next
Dim celltext As String
Dim count As Integer = -1
'add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To DataGridView1.Columns.Count - 1
If DataGridView1.Columns(j).Visible = True Then
count = count + 1
dr1(count) = DataGridView1.Rows(i).Cells(j).Value
End If
Next
count = -1
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Excel.Application
Dim wBook As Excel.Workbook
Dim wSheet As Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
'calculate the sum for "المبلغ" from the datagridview
Dim Result As Double
For i As Integer = 0 To DataGridView1.RowCount - 1
Result += DataGridView1.Rows(i).Cells(0).Value
'Change the number 2 to your column index number (The first column has a 0 index column)
'In this example the column index of Price is 2
Next
'add the sum to sheet
wSheet.Cells(DataGridView1.RowCount + 2, 1) = Result
wSheet.Cells(DataGridView1.RowCount + 2, 2) = "المجموع"
' for the header
wSheet.Rows(1).Font.Name = "Droid Arabic Kufi"
wSheet.Rows(1).Font.size = 11
wSheet.Rows(1).Font.Bold = True
wSheet.Rows(1).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#20b2aa")
wSheet.Rows(1).Font.color = mycol
' for all the sheet without header
wSheet.Range("a2", "z1000").Font.Name = "Droid Arabic Kufi"
wSheet.Range("a2", "z1000").Font.Size = 10
' make the sheet Alignment center
wSheet.Range("a2", "z1000").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
wSheet.Range("A1:X1").EntireColumn.AutoFit()
wSheet.Range("A1:X1").EntireRow.AutoFit()
wSheet.Columns("J").ColumnWidth = 28
'make the first column "المبلغ" format is money
wSheet.Columns("A").NumberFormat = "#,##0_);[Red](#,##0)"
' this add Grid line to all rows and columns
Dim formatRange As Excel.Range = wSheet.UsedRange
Dim cell As Excel.Range = wSheet.Range("a1", "j" & DataGridView1.RowCount + 1 & "")
Dim border As Excel.Borders = cell.Borders
border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
border.Weight = 1.0
' this add header and footer when printing the sheet
wSheet.PageSetup.CenterHeader = "&""Droid Arabic Kufi,Bold""&14الحوالات الصادرة"
wSheet.PageSetup.RightFooter = DateTime.Now
wSheet.PageSetup.LeftFooter = "Page &P of &N"
'make the print page horizontal
wSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape
'make all columns fit in one page
wSheet.PageSetup.Zoom = False
wSheet.PageSetup.FitToPagesWide = 1
wSheet.PageSetup.FitToPagesTall = False
Dim strFileName As String = saveFileDialog1.FileName
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
Exit Sub
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName)
excel.Workbooks.Open(strFileName)
excel.Visible = True
Exit Sub
errorhandler:
MsgBox(Err.Description)
End Sub
My answer to your question is really simple with:
xlWorksheet.PageSetup.PrintGridLines = True
I try to name cells with a macro in Excel. Here is the code :
Sub setHeader()
Dim Wb As Workbook
Dim Ws As Worksheet
Dim Cell As Range
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
i = 0
j = 0
k = 0
l = 0
Dim MyColumnRange As Range
Dim MyRowRange As Range
Dim MyCellRange As Range
Set MyColumnRange = ActiveSheet.Range("E7:V8")
Set MyRowRange = ActiveSheet.Range("B10:B16")
Set MyCellRange = ActiveSheet.Range("E10:V16")
Dim MyColumnName As String
Dim MyRowName As String
Dim MyCellName As String
MyColumnName = "HC_"
MyRowName = "HL_"
MyCellName = "?"
For Each Cell In MyRowRange
Cell.Name = MyRowName + CStr(j)
j = j + 1
Next Cell
For Each Cell In MyColumnRange
If Not IsEmpty(Cell.Value) Then
Cell.Name = MyColumnName + CStr(i)
i = i + 1
End If
Next Cell
For Each Cell In MyCellRange
Cell.Name = MyRowName + CStr(k) + MyCellName + MyColumnName + CStr(l)
If l = (MyCellRange.Columns.Count - 1) Then
l = 0
k = k + 1
Else
l = l + 1
End If
Next Cell
End Sub
I works well for the naming in MyColumnRange and MyCellRange, but for MyRowRange I have the error
"method name of object range failed"
and I do not understand why.
When you are naming cells in MyRowRange:
For Each Cell In MyRowRange
Cell.Name = MyRowName + CStr(i)
j = j + 1
Next Cell
You are naming them with your terribly named variable i but you are incrementing terribly named variable j. Probably you mean Cell.Name = MyRowName + Cstr(j)
so, you don't really need to convert to string, try using & instead of + like this example :
For j = 1 to MyRowRange.cells.count
MyRowRange.cells(j).Name = MyRowName & j
Next j
i changed your for each loop (wich is fine), just for an example of an other way to do it...