question

HoeksteinArnold-7826 avatar image
0 Votes"
HoeksteinArnold-7826 asked LeonLaude commented

Who can help/analyse? tried all kinds of statements but rows are not added/deleted.

Who can help/analyse? tried all kinds of statements but rows are not added/deleted.
Below is not the most effective way in VBA but I had to find out why statements do not process without giving errors.
I am using tables (listobjects) so I do not have to use cell/row adresses that use column-letters or row-number


Function TableResizeInRows(SheetName As String, TableName As String, NumberDataRows As Integer) As String
'NO ERROR BUT ALSO NO RESULT
Dim l_Tableref As ListObject
Dim l_SheetName As Worksheet
Dim l_range As Range
Dim l_ListRows As ListRows
Dim MyNewRow As ListRow
Dim c_ListRows As Long
Dim InsertIndex As Integer
Dim DeleteIndex As Integer
Dim HeaderIsOnRow As Integer
Dim StartWsRow As Integer
Dim EndWsRow As Integer
On Error GoTo ErrorHandler

If (NumberDataRows < 2) Then
TableResizeInRows = "Row 1 not to be deleted, formulas will be deleted too!"
Exit Function
End If

Set l_SheetName = Worksheets(SheetName)
' make goal-worksheet active (set correct context)
l_SheetName.Activate
' Define Table Object
Set l_Tableref = Sheets(SheetName).ListObjects(TableName)
' determine # rows in listobject
Set l_ListRows = l_Tableref.ListRows
c_ListRows = l_Tableref.ListRows.Count
' determine rownumber on which the table is
HeaderIsOnRow = Sheets(SheetName).ListObjects(TableName).HeaderRowRange.Row
' Now we have sheet-rows that should be deleted/inserted
If (c_ListRows > NumberDataRows) Then
' We have to delete rows but it should be (the last row) of the listobject (Table)
' EndWsRow = last row that is to be deleted (lowest row#)
EndWsRow = (HeaderIsOnRow + NumberDataRows) + 1
' StartWsRow = First row that is to be deleted (highest row#)
StartWsRow = HeaderIsOnRow + c_ListRows
For DeleteIndex = StartWsRow To EndWsRow Step -1
'NO ERROR BUT ALSO NO RESULT
' Set l_range = l_SheetName.Range("A" & DeleteIndex)
' Range("A" & DeleteIndex).EntireRow.Delete
Worksheets(SheetName).Rows(DeleteIndex).Delete
'l_Tableref.ListRows(DeleteIndex - HeaderIsOnRow).Delete 'Based on row in table 'results in error 1004
Next
TableResizeInRows = "Success, rows deleted?"
Exit Function
Else
If (c_ListRows < NumberDataRows) Then
' We must insert directly below the listobject (Table)
' StartWsRow = Last row of table after which a row is to be inserted(so last rownumber on sheet that contains tablerow)
StartWsRow = HeaderIsOnRow + c_ListRows
' EndWsRow = Last row after which one more row is to be inserted
EndWsRow = (HeaderIsOnRow + NumberDataRows) - 1
For InsertIndex = StartWsRow To EndWsRow Step 1
'NO ERROR BUT ALSO NO RESULT
Worksheets(SheetName).Rows(InsertIndex).Insert Shift:=xlShiftDown, CopyOrigin:=xlFormatFromLeftOrAbove
Next
TableResizeInRows = "Success, rows inserted?"
Exit Function
End If
End If
TableResizeInRows = "Result unknown"
Exit Function

ErrorExit:
On Error Resume Next
Exit Function

ErrorHandler:
' Public Function bCentralErrorHandler
' see https://stackoverflow.com/questions/19042604/vba-excel-error-handling-especially-in-functions-professional-excel-developm
If bCentralErrorHandler(msMODULE, sSOURCE, , True) Then
Stop
Resume
Else
Resume ErrorExit
End If
End Function

not-supported
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HoeksteinArnold-7826 avatar image
0 Votes"
HoeksteinArnold-7826 answered

Title should contain VBA:
VBA deleting/inserting rows , no errors, no result

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLaude avatar image
0 Votes"
LeonLaude answered LeonLaude edited

Hi,

VBA is currently not supported in the Q&A forums, the supported products are listed over here https://docs.microsoft.com/en-us/answers/products (more to be added later on).

You can ask the experts in the dedicated VBA forum over here:

https://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba

https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral

(Please don't forget to accept helpful replies as answer)

Best regards,
Leon

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Site no longer supported, says:

The following forum(s) have migrated to Microsoft Q&A:All English Microsoft Azure forums, Developing Universal Windows apps, Microsoft Partner Center API, Windows Presentation Foundation (WPF), Windows 10 Installation, Setup, and Deployment, Active Directory Federation Services!
Visit Microsoft Q&A to post new questions.

0 Votes 0 ·
HoeksteinArnold-7826 avatar image
0 Votes"
HoeksteinArnold-7826 answered LeonLaude commented

Site no longer supported, says:

The following forum(s) have migrated to Microsoft Q&A:All English Microsoft Azure forums, Developing Universal Windows apps, Microsoft Partner Center API, Windows Presentation Foundation (WPF), Windows 10 Installation, Setup, and Deployment, Active Directory Federation Services!
Visit Microsoft Q&A to post new questions.

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok, in this case change the tags to the ones mentioned in the old forum :-)

0 Votes 0 ·

Sorry, first time user,
This site does not allow tags concerning VBA (VBA or Visual basic)

0 Votes 0 ·
LeonLaude avatar image LeonLaude HoeksteinArnold-7826 ·

The VB forum has not yet been migrated to Microsoft Q&A, only the forums that was listed, i.e

  • All English Microsoft Azure forums

  • Developing Universal Windows apps

  • Microsoft Partner Center API

  • Windows Presentation Foundation (WPF)

  • Windows 10 Installation, Setup, and Deployment

  • Active Directory Federation Services

So you should be able to ask your question over here:
https://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba

or here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral





0 Votes 0 ·
Show more comments