簡體   English   中英

Excel VBA宏。 如何將絕對單元格引用寫入單元格

[英]Excel VBA macro. How to write an absolute cell reference into a cell

我有一本索引為第一張紙的工作簿。 隨后的每個工作表都是一個潛水日志。 因為我是使用此工作簿的眾多人之一,所以它需要盡可能“自動”(因為人們很懶)...

每個日志都有一個用於“ New Dive”的宏按鈕。 宏會創建一個新的工作表,並用新的工作表編號(潛水編號)對其進行命名,並清除相關數據以備填寫。當前需要手動填寫索引表,但該表已被忽略。

我關閉了宏,但這是我絆倒的最后一步。 我已經嘗試過Activecell.FormulaR1C1 and Cells(r,c) =...越來越近了,但是沒有一塊。 我對此也很陌生。

這是我的代碼

Sub Add_links()
'
' Add_links Macro
' Adds links to the index sheet so it 'fills itself in'... 

' Each dive is on the row 9 more than the dive number (bear in mind each log is 50 higher than the previous)

Dim Divenumber As Double
Dim Rownumber As Double

Range("I7").Select: Divenumber = ActiveCell.FormulaR1C1

' Make Linenumber the same as Divenumber.
' Do a loop of reducing the Linenumber by 50 until it's in the range 1 to 50.
' Add 9 to that and it becomes the row number of the index sheet

Rownumber = Divenumber

Do
Rownumber = Rownumber - 50
Loop While Rownumber > 50

Rownumber = Rownumber + 9

Worksheets("Dive Index").Activate

Range("A" & Rownumber).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "'Dive " & Divenumber & "'!A1"
'Project number (in cell F4)
Range("B" & Rownumber).Select
ActiveCell.FormulaR1C1 = "='Dive " & Divenumber & "'!F4"
'Task(in cell C7)
Range("C" & Rownumber & ":G" & Rownumber).Select
ActiveCell.FormulaR1C1 = "='Dive " & Divenumber & "'!C7"
'Start date (in cell C21)
Range("H" & Rownumber).Select
ActiveCell.FormulaR1C1 = "='Dive " & Divenumber & Chr(39) & "!$C$21"
'Start time (in cell E21)
Range("I" & Rownumber).Select
ActiveCell.FormulaR1C1 = "='Dive " & Divenumber & Chr(39) & "!$E$21"
'End date (in cell F21)
Range("J" & Rownumber & ":K" & Rownumber).Select
ActiveCell.FormulaR1C1 = "='Dive " & Divenumber & Chr(39) & "!$F$21"
'End time (in cell G21)
Range("L" & Rownumber).Select
ActiveCell.FormulaR1C1 = "='Dive " & Divenumber & Chr(39) & "!$G$21"

Sheets("Dive " & Divenumber).Select
Range("A23").Select
End Sub

這個最接近我了。

ActiveCell.FormulaR1C1 = "='Dive " & Divenumber & "'!F4"

但卻在單元格中添加了一些多余的'。看起來像這樣...

='Dive 53'!'F4' (應該是='Dive 53'!F4

這種方法避免使用超鏈接(我覺得很難維護),而使用索引表的BeforeDoubleClick事件來提供等效功能。

這段代碼進入了Dive Index工作表的代碼模塊,因此它拾取了索引上的雙擊事件:

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    '// This function is called whenever the sheet is double-clicked
    '// It checks the value of the target (selected) cell, to see if it is a reference
    '// to a dive sheet. If so, it activates that sheet

    '// Get hold of the contents of the target cell
    Dim sTarget As String: sTarget = Target.Cells(1, 1).Value

    '// Check if it refers to a dive sheet
    If Left(sTarget, 5) = "Dive " Then
        Dim wsTarget As Worksheet
        On Error Resume Next
            '// Try to find the worksheet referred to by the target cell
            Set wsTarget = Worksheets(sTarget)
        On Error GoTo 0

        '// Check that a target sheet was found
        If wsTarget Is Nothing Then
            Exit Sub
        End If

        '// Activate the sheet
        wsTarget.Activate

        '// Cancel the default action for double-click
        Cancel = True

    End If


End Sub

這是您的函數Add_Links()的代碼。 我不確定您如何調用它,但是它與您的示例一樣有效,但我使用了一些更簡單的技術。

Option Explicit

Sub Add_links()

        '
        ' Add_links Macro
        ' Adds links to the index sheet so it 'fills itself in'...

        ' Each dive is on the row 9 more than the dive number (bear in mind each log is 50 higher than the previous)

        Dim Divenumber As Double
        Dim Rownumber As Double


        Dim wsDive As Worksheet
        Set wsDive = ActiveSheet

        '// Dive number is in cell I7 of the sheet
        Divenumber = wsDive.Range("I7").Value

        '// Make sure the sheet name corresponds to the dive number
        Dim sDiveName As String
        sDiveName = "Dive " & Divenumber
        wsDive.Name = sDiveName


        '// Calculate the row number for the index entry
        '-- -- Rownumber = Divenumber Mod 50 + 9
        '// Use below if dive numbers in sheet run from 1 to 50, not 0 to 49
        Rownumber = (Divenumber - 1) Mod 50 + 10

        '// Get a reference to the index sheet
        Dim wsIndex As Worksheet: Set wsIndex = Worksheets("Dive Index")

        '// Get a reference to column A in the index entry row
        Dim rLink As Range: Set rLink = wsIndex.Range("A" & Rownumber)

        '// Put the Dive name into column A
        rLink.Value = sDiveName



        '// Reference data from the dive sheet into the index sheet ================

        '// Project number (in cell F4)
        rLink.Offset(0, 1).Formula = ReferenceToCell(wsDive.Range("F4")) '// Index Column B

        '// Task(in cell C7)
        rLink.Offset(0, 2).Formula = ReferenceToCell(wsDive.Range("C7")) '// Index Column C

        '// Start date (in cell C21)
        rLink.Offset(0, 7).Formula = ReferenceToCell(wsDive.Range("C21")) '// Index Column H

        '// Start time (in cell E21)
        rLink.Offset(0, 8).Formula = ReferenceToCell(wsDive.Range("E21")) '// Index Column I

        '// End date (in cell F21)
        rLink.Offset(0, 9).Formula = ReferenceToCell(wsDive.Range("F21")) '// Index Column J

        '// End time (in cell G21)
        rLink.Offset(0, 11).Formula = ReferenceToCell(wsDive.Range("G21")) '// Index Column L


End Sub

Private Function ReferenceToCell(rCell As Range) As String
    '// This function returns a formula that references the value of the given cell
    ReferenceToCell = "='" & rCell.Parent.Name & "'!" & rCell.Cells(1, 1).Address

End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM