2017-10-25 11 views
1

最初のシートとしてインデックスを持つワークブックがあります。その後の各シートはダイブログです。私はこのワークブックを使用している人の多くですが、できるだけ「自動」にする必要があります。(人が怠けているので)...Excel VBAマクロ。セルに絶対セル参照を書き込む方法

各ログには「New Dive」のマクロボタンがあります。マクロは新しいシートを作成し、新しいシート番号(ダイブ番号)で名前を付け、関連するデータを消去します。現在、インデックスシートは手動で塗りつぶす必要がありますが、無視されます。

私はマクロをクローズしていますが、それは私が困惑している最後のステップです。私はActivecell.FormulaR1C1 and Cells(r,c) =...が近づくように試みましたが、パイの部分はありません。私はこれも非常に新しいです。

はここで...このように見える.. ..これも私に最も近いを取得

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

を自分のコード

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 

ですが、セルに「不要のカップルを追加し

​​(012になるはずです)

+1

'R1C1'参照を使用していないので、' FormulaR1C1'ではなく 'Formula'を使用してください。 – Rory

+0

SelectとActiveCellではなく、Range( "H"&Rownumber).Formula ... 'を使用してください。 – SJR

答えて

0

このアプローチでは、ハイパーリンks(私は維持するのが難しい)と代わりに、インデックスシートのBeforeDoubleClickイベントを使用して同等の機能を提供します。それがインデックスにダブルクリックしてイベントを拾うように

このコードは、ダイブインデックスワークシートのコードモジュールに行く:

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 
+0

ワークブックのダイブ番号が、例えば1-50,51-100などで実行されている場合、行番号の計算にわずかな変更が必要であることに気付きました。「Rownumber = Divenumber Mod 50 + 9」という計算は、ワークブックの最後のダイビング、​​つまりダイブ50、ダイブ100などは、行59の代わりに行9に入ります。これを修正するには、コードは 'Rownumber =(DiveNumber - 1)Mod 50 + 10' – JohnRC

関連する問題