Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Get row of active sheets

0

Hello,

I'm working on something i never done before
And it's for me hard but we try our best to manage it
So i like to ignore support of vba if possible
But it can be asked in a other question soon about it

For now how can i manage the row in column A9, A10 ect....
We have a couple tabs and 1 Master sheet
And i want to add the active row of any of my tabs and add them where the sheetname is located
In my config sheet we have B for ID D for sheetname
Sheetnames are created with vba code, i ask later to change that to just a formula if possible.

So my question is how to add the current row into my config sheet on B with formula if possible, every sheet has code where we save our sheetname and then add the row number
Thank you

Answer
Discuss

Discussion

Hi again Ghost. Sorry but I'm not clear on the sequence of events - is the data added to the Master sheet and copied to both the employee ID tab and the Config tab? Please edit your question to explain, perhaps giving an example. 
John_Ru (rep: 6417) Jul 20, '24 at 7:15 pm
@Ghost
Like John, I too am a bit confused. Also, your file contains functions from newer versions which are not supported in 2016, so I won't be able to help you.
WillieD24 (rep: 587) Jul 20, '24 at 9:32 pm
Hi @John_Ru, Sorry about that, the data is added to the master sheet from all the tabs The rownumber will only be used in the config to get te correct hour that match the tabname and rowID Master sheet does not much just collecting all the data from other tabs. @WillieD24, Oh, really i didn't know i used newer data that won't work on other versions of exel, i'm sorry
GhostofWanted (rep: 48) Jul 21, '24 at 1:33 am
I have updated the file i have managed to add the row into the config sheet with vba code But its really slow, any idea how to solve that?
GhostofWanted (rep: 48) Jul 21, '24 at 5:50 am
Ghost, not sure I understand. I have no time to check today but will look tomorrow hopefully
John_Ru (rep: 6417) Jul 21, '24 at 9:11 am
@Ghost
When creating a file using a newer version of Excel (like 2021 or 365) and using the new functions/formulas/features, just remember that, someone using an "older" version (like me with 2016) won't be able to use the file.
:-)
WillieD24 (rep: 587) Jul 21, '24 at 8:26 pm
@Willie - you're right about using a "newer version" but that shouldn't be a reason for a user to limit their spreadsheet functionailty or ease of developemnt - Excel has introduced some powerful new functions which can save a user lots of time (if they have bought the newer version supporting those). It might however limit the solution offered (or who can) but note that for example that VBA can still be created / work on older versions e.g. you could write a formula to a cell using IFS even if that worksheet function isn't available in your version of Excel.
John_Ru (rep: 6417) Jul 22, '24 at 9:42 am
@John - When I looked at the formulas on the "Master" sheet (cols B thru J) they begin with "xlfn" and "xlws" which indicates it is a formula not supported (recognized) in my version (2016). A file created with a newer version (ie 2021, 2023, 365) will work with older versions provided the file doesn't include the newer formulas (ie: HSTACK, VSTACK, XLOOKUP, and others).
WillieD24 (rep: 587) Jul 22, '24 at 10:26 am
@Ghost - above you said "it's really slow, any idea how to solve that?" and you're right- it is slow but I'm not sure why (or how to fix). The loops aren't long but it's much slower than I'd expect. Will try again later.
John_Ru (rep: 6417) Jul 22, '24 at 10:51 am
Please see my Answer and file.

@Willie - if you try my Answer file on Excel 2016, it will work (like it did on my copy of 2010) but the Master sheet is just a series of errors (from the functions not implemented in that earlier version). You copuld open it up on the web version of 365 to see the results (but the macros don't work online).
John_Ru (rep: 6417) Jul 22, '24 at 2:17 pm
@John - I just tried your file. (macros enabled) When I looked at the Master sheet I can see the additions you made while testing. I then tried adding info to the Jane Doe sheet and all entries on the Master sheet changed to #NAME? (just like in Ghost's file.) Rather curious that it works on your 2010 but not my 2016.
WillieD24 (rep: 587) Jul 22, '24 at 8:23 pm
@Willie - sorry to confuse you! In 2010 the #NAME thing happens but the Jane Doe sheet gets the extra validation in D and entry in J (as you might expect). Let's see what Gost finds.
John_Ru (rep: 6417) Jul 23, '24 at 1:10 am
Add to Discussion

Answers

0
Selected Answer

Ghost

Your speed problem with the file puzzled me for a while then I realised that the problem was the recalculation of formulae (e.g. the SORT spill in your table in the worksheet Master). It adds seconds since it happens every time a cell value is changed by your code.

In the attached revised file, I changed the method to Manual- that makes the changes almost instantaneous but that table isn't updated so, in the code behind JaneDoe (only), I made these changes (in bold):

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim rowNum As Long
    Dim ws As Worksheet
    Dim password As String
    Dim firstRow As Long
    Dim lastRow As Long
    Dim i As Long
    Dim newRow As Range
    Application.EnableEvents = False
    Set ws = Me
    password = "admin" ' Replace with your actual password
    firstRow = 9 ' Starting row for processing

    ' Check if the changed cell is in column C
    If Not Intersect(Target, ws.Columns("C")) Is Nothing Then
        Application.EnableEvents = False
        ' set calc method to manual for speed
        Application.Calculation = xlCalculationManual

        On Error GoTo ErrorHandler

        ' Unprotect the worksheet before making changes
        ws.Unprotect password:=password

        ' << existing code unchanged>>

        Application.EnableEvents = True
        ' recalculate all sheets (will take time to )
        Calculate
        Worksheets("MASTER").Calculate
        Exit Sub

ErrorHandler:
        MsgBox "Error: " & Err.Description, vbExclamation
        Application.EnableEvents = True
        ' Ensure protection is reapplied even if an error occurs
        'ws.Protect password:=password, _
        '           AllowInsertingRows:=True, _
        '           AllowDeletingRows:=True
    End If
End Sub

Now the message appears very soon after you add a value to column C (say) of the Jane Doe sheet (since the recalculation is really done after that message).

You'd need to change that for each sheet (or -better-  have a new common code in a module and pass the sheet name and Target to that new macro).

You could set the method to Automatic (or Semi...) via menu File/ Options/ Formulas/ Calculation Options and add code to capture the method at the start (before Application.Calculation = xlCalculationManual) and restore it at the end if it's not xlManual (when you wouldn't need the Calculate line).

Revision #1 dated 23 July 2024

Further to the point below (code not working after Jane Doe "speed up" elements copied correctly  to Rik), the problem is NOT because of my code changes above but due to flaw in your logic in the module 2 code AddSheetNameToConfig.

If you add a new line to the sheet Rik, your code the associated Worksheet_Change event code calls AddSheetNameToConfig which finds "Rik" at $B$7 of Config then searches from $B$7 to $B$38 looking to match the row number ONLY. It finds a match at $B$13 (for example), which is a Jane Doe entry. You then get a MsgBox to says that already exists (though one doesn't for Rik and the row number!).

The simple fix is to change the following lines within the module 2 code AddSheetNameToConfig (changes in bold):

           ' Sheet name found, now check if the row number exists in column B for this sheet name
            For Each cell In .Range(.Cells(foundSheetName.Row, "B"), .Cells(lastRow, "B"))
                ' check that both row and sheet match
                If cell.Value = currentRow And cell.Offset(0, 1).Value = sheetName Then

I did that in the revised file attached below.

Hope this fixes your problem (or sets you one the right path). Don't forget to mark as Selected if so.

Discuss

Discussion

Hello John_RU Wow that's all beter then mine before, thank you But seems when i use the code from Jane to Rik It gives me an error and says that the id already exists, but i don't see it Any idea how to fix that so it add record if not exists in B and where the tabname is located. Thanks once again, i have update your code
GhostofWanted (rep: 48) Jul 23, '24 at 5:08 am
Ghost- please see Revision #1 dated 23 July 2024 to my Answer and the revised file. Kindly mark the Answer as Selected if that solves things for you.
John_Ru (rep: 6417) Jul 23, '24 at 8:00 am
Ghost - did you see the revised Answer? 
John_Ru (rep: 6417) Jul 24, '24 at 11:21 am
hi John_Ru Yes i did, but seems it add's the row now of Rik sheet But still in the column G it still says NOT FOUND Even the row 13 exists in config Any idea? Thank you
GhostofWanted (rep: 48) Jul 24, '24 at 12:29 pm
Ghost. I believe t hat problem has nothing to do with the VBA in my Answer....

CORRECTED 24 July 2024

The formula in cell G9 (and down) of Rik isn't the same as G9 in Jane Doe- you should change it to read:
=IFERROR(
    INDEX(
        CONFIG!C:C,
        SMALL(
            IF(
                CONFIG!D:D = TRIM(MID(CELL("filename", A1), FIND("]", CELL("filename", A2)) + 1, 255)),
                ROW(CONFIG!D:D) - MIN(ROW(CONFIG!D:D)) + 1,
                ""
            ),
            ROW(A1)
        )
    ),
    "Not Found"
)

like Jane Doe (whereas in Rik currently refers to A2, not A1). Then "Not found" in the added, last row will be replaced by the time.

Hope that fixes things for you (or you check that the formuale do what you want at least) so please Select my Answer.
John_Ru (rep: 6417) Jul 24, '24 at 1:49 pm
I don't know John_RU
Seems the row ID in rik and john aren't working how it must be
in the config sheet it still added them duplicated if they exists
Not sure what we overlooking :( or why it don't work how it should
GhostofWanted (rep: 48) Jul 24, '24 at 4:19 pm
Ghost. I'm not sure. I solved your speed problem and think it's down to you to fix other issues. Sorry but I have no more time to spare on this. 
John_Ru (rep: 6417) Jul 24, '24 at 6:12 pm
Hope you fixed any issues. Thanks for selecting my Answer, Ghost. 
John_Ru (rep: 6417) Jul 25, '24 at 1:21 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login