Excel宏以用ActiveX文本框填充列

时间:2019-01-25 12:21:48

标签: excel vba

我想用Activex文本框(完全适合单元格)填充E1:E10范围,如果调整单元格的大小,它们也会重新调整大小。

我收到“应用程序或对象定义的错误”。

sub pastetextboxes()
For i=1 to 10
  set rng=activesheet.cells(i,5)
  set test=activesheet.oleobjects.add(classtype:="Forms.textbox.1", Link:=false, Displayasicon:=false, Left:=rng.left, top:=rng.top, width:=rng.width,height:=rng.height)
next i

end sub

1 个答案:

答案 0 :(得分:0)

如果您不介意忘记ActiveX控件,则:

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Example extends Application{

    private Group  group;
    private double mousePointX;
    private double mousePointY;

    public static void main(String... arguments){ launch(arguments); }

    @Override public void start(Stage primaryStage){

        group = new Group();

        Pane pane = new Pane(group);
        pane.setOnMousePressed(this::recordPosition);
        pane.setOnMouseDragged(this::makeShape);
        pane.setOnScroll(this::handleScroll);

        primaryStage.setScene(new Scene(new BorderPane(pane)));
        primaryStage.show();
    }

    //record mouse position
    private void recordPosition(MouseEvent event){
        mousePointX = event.getX();
        mousePointY = event.getY();
    }

    //method to make shape
    private void makeShape(MouseEvent event){
        Rectangle object;
        object = new Rectangle(200, 200, Color.BLUE);
        object.setStroke(Color.BLACK);

        group.getChildren().addAll(object);

        object.setTranslateX(mousePointX - group.getTranslateX());
        object.setTranslateY(mousePointY - group.getTranslateY());
        object.setHeight(event.getY() - mousePointY);
        object.setWidth(event.getX() - mousePointX);
    }

    //method to control scroll
    private void handleScroll(ScrollEvent event){
        if(event.isControlDown()){
            zoom(Math.pow(1.01, event.getDeltaY()), event.getSceneX(), event.getSceneY());
        }else{
            group.setTranslateX(this.group.getTranslateX() + event.getDeltaX());
            group.setTranslateY(this.group.getTranslateY() + event.getDeltaY());
        }
        event.consume();
    }

    //method to control zoom
    private void zoom(double factor, double x, double y){
        double oldScale = group.getScaleX();
        double scale    = oldScale * factor;

        if(scale < 0.05) scale = 0.05;
        if(scale > 50) scale = 50;

        group.setScaleX(scale);
        group.setScaleY(scale);

        double f      = (scale / oldScale) - 1;
        Bounds bounds = this.group.localToScene(this.group.getBoundsInLocal());
        double dx     = (x - (bounds.getWidth() / 2 + bounds.getMinX()));
        double dy     = (y - (bounds.getHeight() / 2 + bounds.getMinY()));

        group.setTranslateX(this.group.getTranslateX() - f * dx);
        group.setTranslateY(this.group.getTranslateY() - f * dy);
    }

}

随着列宽或行高的变化,这些形状的大小也会改变。

如果要使用ActiveX控件,可以这样做:

Sub pastetextboxes()

For i = 1 To 10
    With ActiveSheet.Cells(i, 5)
        ActiveSheet.Shapes.AddTextbox msoTextOrientationHorizontal, .Left, .Top, .Width, .Height
    End With
Next i

End Sub

我希望您不会因使用ActiveX控件而遇到fontsize错误。