文本在带有空格VBA Excel的按钮上重复自身

时间:2017-05-25 16:42:19

标签: excel vba

我对VBA很陌生,很少有我真正理解的东西。我试图制作一个按钮,每当我按下它时,应该在原始的下面应用2个单元格,我设法在StackOverflow的某个人的帮助下移动边框,但我也不能使文本也也重置。(我会上传,文件给你看!)

代码:

//bind to all links
$('a').click( function() {
   //get the url
   var url = $(this).prop('href');
   //send the url to your server
   $.ajax({
        type: "POST",
        url: "http://yourserver.com/process.php",
        data: "url=" + url
   });
});

DL LINK: https://mega.nz/#!wwdnlT6A!gxMKYwhjxlX1fjMXPWPhYbK-j-nAIiQ_VbZgdKqBJ-s

1 个答案:

答案 0 :(得分:0)

制作现有工作簿的副本,然后使用我在下面提供的代码替换所有代码。这将允许您引用相对于上次使用的单元格的单元格。我在代码中添加了注释来解释可能看起来很奇怪的事情

Option Explicit         ' ALWAYS USE THIS!!!

Sub DoSix()
Dim lLastRow    As Long
Dim lLastCol    As Long
Dim rng         As Range

    ' If you start with an 'empty' worksheet, the LastRow check gives an error.
    ' So, just ignore the error and initialize the starting row number.
    On Error Resume Next        ' Ignore error for a second
    With ActiveWorkbook.Sheets("Sheet1")
        lLastRow = Cells.Find(What:="*", after:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        lLastCol = Cells.Find(What:="*", after:=Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    End With
    If Err.Number = 91 Then
        lLastRow = 4
    Else
        lLastRow = lLastRow + 3
    End If
    On Error GoTo 0

    ' Reference all the cells as relative to where you want to be
    DoBorders Range("A" & lLastRow & ":P" & lLastRow + 5)
    Range("A" & lLastRow).Value = "Bought At"
    Range("B" & lLastRow).Value = "NR"
    Range("C" & lLastRow).Value = "Name of Stock"
    Range("D" & lLastRow).Value = "PB"
    Range("E" & lLastRow).Value = "PA"
    Range("F" & lLastRow).Value = "PR"
    Range("H" & lLastRow).Value = "Name"
    Range("I" & lLastRow).Value = "1"
    Range("J" & lLastRow).Value = "2"
    Range("K" & lLastRow).Value = "3"
    Range("L" & lLastRow).Value = "4"
    Range("M" & lLastRow).Value = "5"
    Range("N" & lLastRow).Value = "Data"
    Range("B" & lLastRow + 1).Value = "1"
    Range("B" & lLastRow + 2).Value = "2"
    Range("B" & lLastRow + 3).Value = "3"
    Range("B" & lLastRow + 4).Value = "4"
    Range("B" & lLastRow + 5).Value = "5"
    Range("H" & lLastRow + 1).Value = "Price"
    Range("H" & lLastRow + 2).Value = "Price"
    Range("H" & lLastRow + 3).Value = "Price"
    Range("H" & lLastRow + 4).Value = "Price"
    Range("H" & lLastRow + 5).Value = "Price"
    lLastRow = lLastRow + 5
End Sub

Sub ResetStart()
    DoBorders Nothing
End Sub

Sub DoBorders(rng As Range)
Dim useRange        As Range
Static lastRange    As Range

    On Error GoTo Error_Trap

    'handle resetting the "last range"
    If rng Is Nothing Then
        Set lastRange = Nothing
        Exit Sub
    End If

    ' Again, deleting rows (from 3 down), while testing, causes an error. So let's handle that.
    On Error Resume Next
    Debug.Print lastRange.Row
    If Err.Number = 424 Then            ' If error, set lastRange to Nothing and all is OK.
        Set lastRange = Nothing
    End If
    On Error GoTo Error_Trap

    If lastRange Is Nothing Then
        Set useRange = rng
    Else
        Set useRange = lastRange.Cells(1).Offset(lastRange.Rows.Count + 2, 0) _
                                   .Resize(rng.Rows.Count, rng.Columns.Count)
    End If

    Set lastRange = useRange
    With useRange
        .Borders.LineStyle = xlNone 'remove all borders
        With .Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlThick
        End With
        With .Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlThick
        End With
    End With

    Exit Sub
Error_Trap:
    Debug.Print Err.Number & vbTab & Err.Description
    Exit Sub
    Resume          ' For debugging
End Sub